Chinaunix首页 | 论坛 | 博客
  • 博客访问: 410878
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-23 10:08:32


HttpClientUtil.java

  1. package com.vibexie.jianai.Utils;

  2. import android.os.Handler;
  3. import android.os.Message;

  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.NameValuePair;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.params.BasicHttpParams;
  10. import org.apache.http.params.HttpConnectionParams;
  11. import org.apache.http.params.HttpParams;
  12. import org.apache.http.util.EntityUtils;

  13. import java.io.IOException;
  14. import java.io.UnsupportedEncodingException;
  15. import java.util.List;

  16. /**
  17.  * HttpClient的工具类
  18.  * Created by vibexie on 4/17/15.
  19.  *
  20.  * 这个工具类可能使用起来比较麻烦,但是它解决了网络连接超时的问题,使得UI主线程不会因为网络连接时间过长而产生ANR
  21.  */
  22. public class HttpClientUtil {

  23.     /**
  24.      * post请求方法,注意这个方法里自动创建了一条线程
  25.      * @param url url地址
  26.      * @param params 请求参数
  27.      * @param handler 关联的handler
  28.      */
  29.     public static void doPost(final String url,final List<NameValuePair> params,final Handler handler){
  30. /****************************** 使用说明 **************************/
  31. //
  32. // /**
  33. // * 构建请求url
  34. // */
  35. // String url=ServerConf.SERVER_ADDR+"JianaiServer/RegisterServlet";
  36. //
  37. // /**
  38. // * 构建url请求参数
  39. // */
  40. // List params=new ArrayList();
  41. // params.add(new BasicNameValuePair("cmd", RegisterCmd.REQUEST_VERIFING_CODE));
  42. // params.add(new BasicNameValuePair("info","vibexie@qq.com"));
  43. //
  44. // /**
  45. // * 构建handler,在handler中获取并处理结果
  46. // */
  47. // Handler handler=new Handler(){
  48. // @Override
  49. // public void handleMessage(Message msg) {
  50. // super.handleMessage(msg);
  51. // String result=(String)msg.obj;
  52. // Toast.makeText(RegisterForVerifyingCodeActivity.this,result,Toast.LENGTH_SHORT).show();
  53. //
  54. // }
  55. // };
  56. //
  57. // /**
  58. // * 调用请求方法
  59. // */
  60. // HttpClientUtil.doPost(url, params,handler);
  61. /****************************** 使用说明 **************************/

  62.         /**
  63.          * 创建新线程
  64.          */
  65.         new Thread(new Runnable() {

  66.             @Override
  67.             public void run() {
  68.                 /**
  69.                  * 返回的结果
  70.                  */
  71.                 String result=null;

  72.                 /**
  73.                  * 国外程序员使用的超时方案
  74.                  */
  75.                 HttpParams httpParams=new BasicHttpParams();
  76.                 HttpConnectionParams.setConnectionTimeout(httpParams,20000);
  77.                 HttpConnectionParams.setSoTimeout(httpParams,20000);

  78.                 DefaultHttpClient httpClient= new DefaultHttpClient(httpParams);

  79.                 /**
  80.                  * 也可以这样设置超时
  81.                  */
  82. // /**
  83. // * 设置请求超时60秒
  84. // */
  85. // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,10000);
  86. //
  87. // /**
  88. // * 设置数据传输超时60秒
  89. // */
  90. // httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,10000);

  91.                 HttpPost httpPost=new HttpPost(url);

  92.                 /**
  93.                  * 设置请求参数
  94.                  */
  95.                 UrlEncodedFormEntity entity= null;
  96.                 try {
  97.                     entity = new UrlEncodedFormEntity(params,"UTF-8");
  98.                 } catch (UnsupportedEncodingException e) {
  99.                     e.printStackTrace();
  100.                 }
  101.                 httpPost.setEntity(entity);

  102.                 /**
  103.                  * 执行请求并获得返回结果
  104.                  */
  105.                 try {
  106.                     HttpResponse httpResponse=httpClient.execute(httpPost);

  107.                     if(httpResponse.getStatusLine().getStatusCode()==200){
  108.                         result=EntityUtils.toString(httpResponse.getEntity());
  109.                     }

  110.                 } catch (IOException e) {
  111.                     e.printStackTrace();
  112.                 } finally {
  113.                     /**
  114.                      * 关闭httpClient,这也是不将httpClient设置为类的静态属性的原因
  115.                      *
  116.                      * 特别注意,不能在解析httpResponse.getEntity()前关闭连接,这里非常容易犯错
  117.                      */
  118.                     httpClient.getConnectionManager().shutdown();
  119.                 }

  120.                 /**
  121.                  * 未返回结果,请求超时或服务器关闭,置result为refused
  122.                  */
  123.                 if(result==null){
  124.                     result="refused";
  125.                 }

  126.                 /**
  127.                  * 发送返回结果给handler
  128.                  */
  129.                 Message message=Message.obtain();
  130.                 message.obj=result;
  131.                 handler.sendMessage(message);
  132.             }
  133.         }).start();
  134.     }
  135. }


阅读(2431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~