Android HttpClient GET或者POST请求基本使用方法
程序员文章站
2023-12-04 11:07:40
在android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此android的sdk提供了apache的httpclient来方便我们使用各种http服务。你...
在android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此android的sdk提供了apache的httpclient来方便我们使用各种http服务。你可以把httpclient想象成一个浏览器,通过它的api我们可以很方便的发出get,post请求(当然它的功能远不止这些)
这里只介绍如何使用httpclient发起get或者post请求
get 方式
//先将参数放入list,再对参数进行url编码
list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair("param1", "中国"));
params.add(new basicnamevaluepair("param2", "value2"));
//对参数编码
string param = urlencodedutils.format(params, "utf-8");
//baseurl
string baseurl = "http://ubs.free4lab.com/php/method.php";
//将url与参数拼接
httpget getmethod = new httpget(baseurl + "?" + param);
httpclient httpclient = new defaulthttpclient();
try {
httpresponse response = httpclient.execute(getmethod); //发起get请求
log.i(tag, "rescode = " + response.getstatusline().getstatuscode()); //获取响应码
log.i(tag, "result = " + entityutils.tostring(response.getentity(), "utf-8"));//获取服务器响应内容
} catch (clientprotocolexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
post方式
//和get方式一样,先将参数放入list
params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair("param1", "post方法"));
params.add(new basicnamevaluepair("param2", "第二个参数"));
try {
httppost postmethod = new httppost(baseurl);
postmethod.setentity(new urlencodedformentity(params, "utf-8")); //将参数填入post entity中
httpresponse response = httpclient.execute(postmethod); //执行post方法
log.i(tag, "rescode = " + response.getstatusline().getstatuscode()); //获取响应码
log.i(tag, "result = " + entityutils.tostring(response.getentity(), "utf-8")); //获取响应内容
} catch (unsupportedencodingexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (clientprotocolexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
这里只介绍如何使用httpclient发起get或者post请求
get 方式
复制代码 代码如下:
//先将参数放入list,再对参数进行url编码
list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair("param1", "中国"));
params.add(new basicnamevaluepair("param2", "value2"));
//对参数编码
string param = urlencodedutils.format(params, "utf-8");
//baseurl
string baseurl = "http://ubs.free4lab.com/php/method.php";
//将url与参数拼接
httpget getmethod = new httpget(baseurl + "?" + param);
httpclient httpclient = new defaulthttpclient();
try {
httpresponse response = httpclient.execute(getmethod); //发起get请求
log.i(tag, "rescode = " + response.getstatusline().getstatuscode()); //获取响应码
log.i(tag, "result = " + entityutils.tostring(response.getentity(), "utf-8"));//获取服务器响应内容
} catch (clientprotocolexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
post方式
复制代码 代码如下:
//和get方式一样,先将参数放入list
params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair("param1", "post方法"));
params.add(new basicnamevaluepair("param2", "第二个参数"));
try {
httppost postmethod = new httppost(baseurl);
postmethod.setentity(new urlencodedformentity(params, "utf-8")); //将参数填入post entity中
httpresponse response = httpclient.execute(postmethod); //执行post方法
log.i(tag, "rescode = " + response.getstatusline().getstatuscode()); //获取响应码
log.i(tag, "result = " + entityutils.tostring(response.getentity(), "utf-8")); //获取响应内容
} catch (unsupportedencodingexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (clientprotocolexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}