Android开发实现HttpClient工具类

本文出自:http://xierh21.iteye.com/blog/1236654 ,谢谢作者给我们提供这么的文章,谢谢!

在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务。你可以把HttpClient想象成一个浏览器,通过它的API我们可以很方便的发出GET,POST请求(当然它的功能远不止这些)。
  比如你只需以下几行代码就能发出一个简单的GET请求并打印响应结果:

try {
          // 创建一个默认的HttpClient
          HttpClient httpclient = new DefaultHttpClient();
          // 创建一个GET请求
          HttpGet request = new HttpGet("www.google.com");
          // 发送GET请求,并将响应内容转换成字符串
          String response = httpclient.execute(request, new BasicResponseHandler());
          Log.v("response text", response);
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }

为什么要使用单例HttpClient?
  这只是一段演示代码,实际的项目中的请求与响应处理会复杂一些,并且还要考虑到代码的容错性,但是这并不是本篇的重点。注意代码的第三行:

HttpClient httpclient = new DefaultHttpClient();
在发出HTTP请求前,我们先创建了一个HttpClient对象。那么,在实际项目中,我们很可能在多处需要进行HTTP通信,这时候我们不需要为每个请求都创建一个新的HttpClient。因为之前已经提到,HttpClient就像一个小型的浏览器,对于整个应用,我们只需要一个HttpClient就够了。看到这里,一定有人心里想,这有什么难的,用单例啊!!就像这样:

public class CustomerHttpClient {
      private static HttpClient customerHttpClient;
    
      private CustomerHttpClient() {
      }
    
      public static HttpClient getHttpClient() {
          if(null == customerHttpClient) {
              customerHttpClient = new DefaultHttpClient();
          }
          return customerHttpClient;
      }
  }


  多线程!试想,现在我们的应用程序使用同一个HttpClient来管理所有的Http请求,一旦出现并发请求,那么一定会出现多线程的问题。这就好像我们的浏览器只有一个标签页却有多个用户,A要上google,B要上baidu,这时浏览器就会忙不过来了。幸运的是,HttpClient提供了创建线程安全对象的API,帮助我们能很快地得到线程安全的“浏览器”。

  解决多线程问题

public class CustomerHttpClient {
      private static final String CHARSET = HTTP.UTF_8;
      private static HttpClient customerHttpClient;

      private CustomerHttpClient() {
      }

      public static synchronized HttpClient getHttpClient() {
          if (null == customerHttpClient) {
              HttpParams params = new BasicHttpParams();
              // 设置一些基本参数
              HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
              HttpProtocolParams.setContentCharset(params,
                      CHARSET);
              HttpProtocolParams.setUseExpectContinue(params, true);
              HttpProtocolParams
                      .setUserAgent(
                              params,
                              "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
                                      + "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
              // 超时设置
              /* 从连接池中取连接的超时时间 */
              ConnManagerParams.setTimeout(params, 1000);
              /* 连接超时 */
              HttpConnectionParams.setConnectionTimeout(params, 2000);
              /* 请求超时 */
              HttpConnectionParams.setSoTimeout(params, 4000);
            
              // 设置我们的HttpClient支持HTTP和HTTPS两种模式
              SchemeRegistry schReg = new SchemeRegistry();
              schReg.register(new Scheme("http", PlainSocketFactory
                      .getSocketFactory(), 80));
              schReg.register(new Scheme("https", SSLSocketFactory
                      .getSocketFactory(), 443));

              // 使用线程安全的连接管理来创建HttpClient
              ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
                      params, schReg);
              customerHttpClient = new DefaultHttpClient(conMgr, params);
          }
          return customerHttpClient;
      }
  }

在上面的getHttpClient()方法中,我们为HttpClient配置了一些基本参数和超时设置,然后使用ThreadSafeClientConnManager来创建线程安全的HttpClient。上面的代码提到了3种超时设置,比较容易搞混,故在此特作辨析



    /**
     * Using GET method.
     *
     * @param url
     *            The remote URL.
     * @param queryString
     *            The query string containing parameters
     * @return Response string.
     * @throws Exception
     */
    public String httpGet(String url, String queryString) throws Exception {
        String responseData = null;

        if (queryString != null && !queryString.equals("")) {
            url += "?" + queryString;
        }

        HttpClient httpClient = new HttpClient();
        GetMethod httpGet = new GetMethod(url);
        httpGet.getParams().setParameter("http.socket.timeout",
                new Integer(CONNECTION_TIMEOUT));

        try {
            int statusCode = httpClient.executeMethod(httpGet);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("HttpGet Method failed: "
                        + httpGet.getStatusLine());
            }
            // Read the response body.
            responseData = httpGet.getResponseBodyAsString();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            httpGet.releaseConnection();
            httpClient = null;
        }

        return responseData;
    }

    /**
     * Using POST method.
     *
     * @param url
     *            The remote URL.
     * @param queryString
     *            The query string containing parameters
     * @return Response string.
     * @throws Exception
     */
    public String httpPost(String url, String queryString) throws Exception {
        String responseData = null;
        HttpClient httpClient = new HttpClient();
        PostMethod httpPost = new PostMethod(url);
        httpPost.addParameter("Content-Type",
                "application/x-www-form-urlencoded");
        httpPost.getParams().setParameter("http.socket.timeout",
                new Integer(CONNECTION_TIMEOUT));
        if (queryString != null && !queryString.equals("")) {
            httpPost.setRequestEntity(new ByteArrayRequestEntity(queryString
                    .getBytes()));
        }

        try {
            int statusCode = httpClient.executeMethod(httpPost);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("HttpPost Method failed: "
                        + httpPost.getStatusLine());
            }
            responseData = httpPost.getResponseBodyAsString();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            httpPost.releaseConnection();
            httpClient = null;
        }

        return responseData;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值