Android HttpClient GET或者POST请求基本使用方法
时间:2021-11-18 04:26:57
在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,POST请求
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java线程同步方法实例总结
2022-08-20 20:35:08
Android实现局部图片滑动指引效果示例
2022-10-16 03:05:53
![](https://img.aspxhome.com/file/2023/3/109153_0s.png)
android中Activity详解(生命周期、以各种方式启动Activity、状态保存,完全退出等)
2023-11-28 07:28:04
![](https://img.aspxhome.com/file/2023/3/137523_0s.png)
小白2分钟学会Visual Studio如何将引用包打包到NuGet上
2022-01-14 10:25:53
![](https://img.aspxhome.com/file/2023/9/70439_0s.jpg)
C#文件上传与下载的实现方法
2023-08-19 04:18:50
在WinForm中发送HTTP请求的实现方法
2023-01-28 10:47:35
java过滤特殊字符操作(xss攻击解决方案)
2022-09-27 13:48:59
Java线程中sleep和wait的区别详细介绍
2021-08-27 02:23:12
Java设计模式之迭代模式(Iterator模式)介绍
2022-07-24 16:03:29
java.nio.file.WatchService 实时监控文件变化的示例代码
2021-10-03 01:52:53
![](https://img.aspxhome.com/file/2023/8/64108_0s.jpg)
java多线程之停止线程的方法实例代码详解
2023-03-23 04:35:21
Windows下C#的GUI窗口程序中实现调用Google Map的实例
2022-01-10 10:31:07
![](https://img.aspxhome.com/file/2023/3/82193_0s.jpg)
Maven将代码及依赖打成一个Jar包的方式详解(最新推荐)
2022-03-31 06:52:47
![](https://img.aspxhome.com/file/2023/3/103843_0s.png)
IntelliJ IDEA 好用插件之analyze inspect code详解
2021-09-26 22:16:36
![](https://img.aspxhome.com/file/2023/9/67649_0s.png)
C# 抓取网页内容的方法
2022-09-13 04:50:10
解析C#中[],List,Array,ArrayList的区别及应用
2022-07-29 16:24:10
ssm框架下web项目,web.xml配置文件的作用(详解)
2021-07-31 14:42:11
C/C++在Java、Android和Objective-C三大平台下实现混合编程
2022-01-04 16:58:08
![](https://img.aspxhome.com/file/2023/2/102612_0s.jpg)
基于Java实现收发电子邮件功能
2021-08-23 17:30:14
![](https://img.aspxhome.com/file/2023/7/80307_0s.png)
jdbc和mybatis的流式查询使用方法
2023-08-24 15:39:25