Android发送GET与POST请求的DEMO详解
程序员文章站
2023-10-19 14:00:06
4.0后网络访问必须单独起一个子线程访问,否则无法运行,这里有一个发送请求的工具类getpostutil复制代码 代码如下:public class getpostutil...
4.0后网络访问必须单独起一个子线程访问,否则无法运行,这里有一个发送请求的工具类getpostutil
public class getpostutil
{
/**
* 向指定url发送get方法的请求
*
* @param url
* 发送请求的url
* @param params
* 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return url所代表远程资源的响应
*/
public static string sendget(string url, string params)
{
string result = "";
bufferedreader in = null;
try
{
string urlname = url + "?" + params;
url realurl = new url(urlname);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty("accept", "*/*");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("user-agent",
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1)");
// 建立实际的连接
conn.connect();
// 获取所有响应头字段
map<string, list<string>> map = conn.getheaderfields();
// 遍历所有的响应头字段
for (string key : map.keyset())
{
system.out.println(key + "--->" + map.get(key));
}
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader(
new inputstreamreader(conn.getinputstream()));
string line;
while ((line = in.readline()) != null)
{
result += "\n" + line;
}
}
catch (exception e)
{
system.out.println("发送get请求出现异常!" + e);
e.printstacktrace();
}
// 使用finally块来关闭输入流
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (ioexception ex)
{
ex.printstacktrace();
}
}
return result;
}
/**
* 向指定url发送post方法的请求
*
* @param url
* 发送请求的url
* @param params
* 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return url所代表远程资源的响应
*/
public static string sendpost(string url, string params)
{
printwriter out = null;
bufferedreader in = null;
string result = "";
try
{
url realurl = new url(url);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty("accept", "*/*");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("user-agent",
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1)");
// 发送post请求必须设置如下两行
conn.setdooutput(true);
conn.setdoinput(true);
// 获取urlconnection对象对应的输出流
out = new printwriter(conn.getoutputstream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader(
new inputstreamreader(conn.getinputstream()));
string line;
while ((line = in.readline()) != null)
{
result += "\n" + line;
}
}
catch (exception e)
{
system.out.println("发送post请求出现异常!" + e);
e.printstacktrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (ioexception ex)
{
ex.printstacktrace();
}
}
return result;
}
}
activity类代码
public class getpostmain extends activity
{
button get , post;
edittext show;
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
get = (button) findviewbyid(r.id.get);
post = (button) findviewbyid(r.id.post);
show = (edittext)findviewbyid(r.id.show);
//利用handler更新ui
final handler h = new handler(){
@override
public void handlemessage(message msg) {
if(msg.what==0x123){
show.settext(msg.obj.tostring());
}
}
};
get.setonclicklistener(new onclicklistener()
{
@override
public void onclick(view v)
{
new thread(new accessnetwork("get", "http://192.168.1.88:8080/abc/a.jsp", null, h)).start();
}
});
post.setonclicklistener(new onclicklistener()
{
@override
public void onclick(view v)
{
new thread(new accessnetwork("post", "http://192.168.1.88:8080/abc/login.jsp", "name=crazyit.org&pass=leegang", h)).start();
}
});
}
}
class accessnetwork implements runnable{
private string op ;
private string url;
private string params;
private handler h;
public accessnetwork(string op, string url, string params,handler h) {
super();
this.op = op;
this.url = url;
this.params = params;
this.h = h;
}
@override
public void run() {
message m = new message();
m.what = 0x123;
if(op.equals("get")){
log.i("iiiiiii","发送get请求");
m.obj = getpostutil.sendget(url, params);
log.i("iiiiiii",">>>>>>>>>>>>"+m.obj);
}
if(op.equals("post")){
log.i("iiiiiii","发送post请求");
m.obj = getpostutil.sendpost(url, params);
log.i("gggggggg",">>>>>>>>>>>>"+m.obj);
}
h.sendmessage(m);
}
}
复制代码 代码如下:
public class getpostutil
{
/**
* 向指定url发送get方法的请求
*
* @param url
* 发送请求的url
* @param params
* 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return url所代表远程资源的响应
*/
public static string sendget(string url, string params)
{
string result = "";
bufferedreader in = null;
try
{
string urlname = url + "?" + params;
url realurl = new url(urlname);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty("accept", "*/*");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("user-agent",
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1)");
// 建立实际的连接
conn.connect();
// 获取所有响应头字段
map<string, list<string>> map = conn.getheaderfields();
// 遍历所有的响应头字段
for (string key : map.keyset())
{
system.out.println(key + "--->" + map.get(key));
}
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader(
new inputstreamreader(conn.getinputstream()));
string line;
while ((line = in.readline()) != null)
{
result += "\n" + line;
}
}
catch (exception e)
{
system.out.println("发送get请求出现异常!" + e);
e.printstacktrace();
}
// 使用finally块来关闭输入流
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (ioexception ex)
{
ex.printstacktrace();
}
}
return result;
}
/**
* 向指定url发送post方法的请求
*
* @param url
* 发送请求的url
* @param params
* 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return url所代表远程资源的响应
*/
public static string sendpost(string url, string params)
{
printwriter out = null;
bufferedreader in = null;
string result = "";
try
{
url realurl = new url(url);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty("accept", "*/*");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("user-agent",
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1)");
// 发送post请求必须设置如下两行
conn.setdooutput(true);
conn.setdoinput(true);
// 获取urlconnection对象对应的输出流
out = new printwriter(conn.getoutputstream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader(
new inputstreamreader(conn.getinputstream()));
string line;
while ((line = in.readline()) != null)
{
result += "\n" + line;
}
}
catch (exception e)
{
system.out.println("发送post请求出现异常!" + e);
e.printstacktrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (ioexception ex)
{
ex.printstacktrace();
}
}
return result;
}
}
activity类代码
复制代码 代码如下:
public class getpostmain extends activity
{
button get , post;
edittext show;
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
get = (button) findviewbyid(r.id.get);
post = (button) findviewbyid(r.id.post);
show = (edittext)findviewbyid(r.id.show);
//利用handler更新ui
final handler h = new handler(){
@override
public void handlemessage(message msg) {
if(msg.what==0x123){
show.settext(msg.obj.tostring());
}
}
};
get.setonclicklistener(new onclicklistener()
{
@override
public void onclick(view v)
{
new thread(new accessnetwork("get", "http://192.168.1.88:8080/abc/a.jsp", null, h)).start();
}
});
post.setonclicklistener(new onclicklistener()
{
@override
public void onclick(view v)
{
new thread(new accessnetwork("post", "http://192.168.1.88:8080/abc/login.jsp", "name=crazyit.org&pass=leegang", h)).start();
}
});
}
}
class accessnetwork implements runnable{
private string op ;
private string url;
private string params;
private handler h;
public accessnetwork(string op, string url, string params,handler h) {
super();
this.op = op;
this.url = url;
this.params = params;
this.h = h;
}
@override
public void run() {
message m = new message();
m.what = 0x123;
if(op.equals("get")){
log.i("iiiiiii","发送get请求");
m.obj = getpostutil.sendget(url, params);
log.i("iiiiiii",">>>>>>>>>>>>"+m.obj);
}
if(op.equals("post")){
log.i("iiiiiii","发送post请求");
m.obj = getpostutil.sendpost(url, params);
log.i("gggggggg",">>>>>>>>>>>>"+m.obj);
}
h.sendmessage(m);
}
}
推荐阅读
-
Android下通过httpClient发送GET和POST请求的实例代码
-
C#模拟http 发送post或get请求的简单实例
-
php发送get、post请求的6种方法简明总结
-
python的get和post方式请求详解
-
Linux下模拟http的get/post请求(curl or wget)详解
-
python通过get,post方式发送http请求和接收http响应的方法
-
详解使用fetch发送post请求时的参数处理
-
postman的安装与使用方法(模拟Get和Post请求)
-
关于Ajax的get与post浅分析,同步请求与异步请求;
-
Android发送GET与POST请求的DEMO详解