Android HttpClient GET或者POST请求基本使用方法
来源:互联网 发布:淘宝免费流量获取途径 编辑:程序博客网 时间:2024/05/15 20:26
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpClient发起GET或者POST请求
在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();
}
原文:http://www.jb51.net/article/32559.htm
0 0
- Android HttpClient GET或者POST请求基本使用方法
- Android HttpClient GET或者POST请求基本使用方法
- Android HttpClient GET或者POST请求基本使用方法
- Android HttpClient基本使用方法!!--get和post
- Android 实现HttpClient发起GET或者POST请求
- HttpClient发起GET或者POST请求
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- android:HttpClient请求(get、post)
- httpClient post/get请求。
- HttpClient get,Post请求
- HttpClient get、post请求
- cocos2d-x httpclient发送get或者post消息请求
- android HttpClient get请求与post请求工具类
- Android--httpclient模拟post请求和get请求
- httpClient----Post 和 GET请求
- HTTPCLIENT POST GET请求模拟
- HTTPClient get post 请求方式
- php数组操作函数(合并,拆分,追加,查找,删除
- <Boost> 正則表達式boost::regex
- 博客迁移
- 23自定义日志工具(分割线)
- Visual SVN的hook脚本(windows版)
- Android HttpClient GET或者POST请求基本使用方法
- hdu1166敌兵布阵
- 这款应用能让你与同一WiFi网络的人匿名聊天
- xx.pch': No such file or directory错误解决
- ADI 15款常用的运算放大器的对比学习
- 即时成像相机有了新发展 收据纸上印照片
- 12.25
- C#中的委托和事件
- debug模式进行调试时出现:reason:absent line number informat错误