Apache HttpClient
2016-08-27 20:44
393 查看
采用Apache HttpClient与远程服务器通信。为了简化HttpClient的用法,定义了一个工具类对HttpClient进行封装该工具类定义了如下两个方法来发送请求
getRequest():发送Get请求
postRequest():发送post请求
package org.crazyit.auction.client.util; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new DefaultHttpClient(); public static final String BASE_URL = "http://192.168.1.88:8888/auction/android/"; /** * * @param url 发送请求的URL * @return 服务器响应字符串 * @throws Exception */ public static String getRequest(final String url) throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { // 创建HttpGet对象。 HttpGet get = new HttpGet(url); // 发送GET请求 HttpResponse httpResponse = httpClient.execute(get); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine() .getStatusCode() == 200) { // 获取服务器响应字符串 String result = EntityUtils .toString(httpResponse.getEntity()); return result; } return null; } }); new Thread(task).start(); return task.get(); } /** * @param url 发送请求的URL * @param params 请求参数 * @return 服务器响应字符串 * @throws Exception */ public static String postRequest(final String url , final Map<String ,String> rawParams)throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { // 创建HttpPost对象。 HttpPost post = new HttpPost(url); // 如果传递参数个数比较多的话可以对传递的参数进行封装 List<NameValuePair> params = new ArrayList<NameValuePair>(); for(String key : rawParams.keySet()) { //封装请求参数 params.add(new BasicNameValuePair(key , rawParams.get(key))); } // 设置请求参数 post.setEntity(new UrlEncodedFormEntity( params, "gbk")); // 发送POST请求 HttpResponse httpResponse = httpClient.execute(post); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine() .getStatusCode() == 200) { // 获取服务器响应字符串 String result = EntityUtils .toString(httpResponse.getEntity()); return result; } return null; } }); new Thread(task).start(); return task.get(); } }
</pre><pre name="code" class="java">
补充说明:
<span style="white-space:pre"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 28px; text-align: center;">Java并发编程:Callable、Future和FutureTask</span></span>
<span style="white-space:pre"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 28px; text-align: center;"><span style="white-space:pre"> </span> 请查看<span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 28px;">http://www.cnblogs.com/dolphin0520/p/3949310.html</span></span></span>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- 小心服务器内存居高不下的元凶--WebAPI服务
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 运维入门
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器