欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android之HttpPost&HttpGet使用方法介绍

程序员文章站 2023-11-04 14:33:16
直接讲用法,先知道怎么用,再知道怎么回事 1、httppost 复制代码 代码如下: try{ //创建连接 httpclient httpclient = new def...
直接讲用法,先知道怎么用,再知道怎么回事

1、httppost
复制代码 代码如下:

try{
//创建连接
httpclient httpclient = new defaulthttpclient();
httppost post = new httppost(url);
//设置参数,仿html表单提交
list<namevaluepair> paramlist = new arraylist<namevaluepair>();
basicnamevaluepair param = new basicnamevaluepair("param1",paramvalue);
paramlist.add(param);

post.setentity(new urlenodedformentity(paramlist,http.utf-8));
//发送httppost请求,并返回httpresponse对象
httpresponse httpresponse = httpclient.execute(post);
// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求
if(httpresponse.getstatusline().getstatuscode() == 200){
//获取返回结果
string result = entityutils.tostring(httpresponse.getentity());
}
}catch(exception e){}

2、httpget
复制代码 代码如下:

try{
httpclient httpclient = new defaulthttpclient();
//仿地址链接直接跟参数,如:http://127.0.0.1:8080/test/test.php?name=;
httpget httpget = new httpget(url);
httpresponse httpresponse = httpclient.execute(httpget);
if(httpresponse.getstatusline().getstatuscode()==200){
string result = entityutils.tostring(httpresponse.getentity());
}
}catch(exception e){}