java所有基础知识Android开发经验谈Android开发

HttpClient你认为就是几句代码就解决了??

2016-06-19  本文已影响579人  脑袋君

HttpClient你认为就是几句代码就解决了??如果你这样想,那你只是处于网络学习阶段了(能够完成网络请求的功能而已),还没有达到项目级别,虽然现在Android studio中HttpClient已经不让开发者使用,但是HttpClient的使用范围依然广泛,不说空话了,直接贴代码出来看看。

这就是那简单的几段代码,就能够完成HttpClient的GET请求:

try{

         //创建一个默认的HttpClient

           HttpClient httpclient=new DefaultHttpClient();

          //创建一个GET请求

            HttpGet request=new HttpGet("www.nsusoft.com");

             //发送GET请求

            HttpResponse response = httpClient.execute(request);

}catch(Exception e) {

           e.printStackTrace();

}

但是这段代码中:

HttpClient  httpclient = new DefaultHttpClient();

    这段代码,让我们没有考虑到真实环境,如代码的容错等等

    其实在发送HTTP通信请求是,很可能在很多地方都能够运用到,我们其实不用每个请求都在创建一个HttpClient,HttpClient就像一个小的浏览器,对整个应用只需要,创建一个HttpClient就够了。

packagecom.caishaohua.internet.request;

importorg.apache.http.client.HttpClient;

importorg.apache.http.impl.client.DefaultHttpClient;

/**

* Created by Administrator on 2016/6/19.

*/

public classCustomerHttpClient {

         private staticHttpClientcustomerHttpClient;

         privateCustomerHttpClient(){}

         public staticHttpClient getHttpClient(){

                if(null==customerHttpClient){

                      customerHttpClient=newDefaultHttpClient();

               }

          returncustomerHttpClient;

        }

}

但是当我们需要有多个请求咋办?我们会很容易想到使用多线程来完成,我们的应用程序使用同一个HttpClient来管理所有的Http请求,一旦出现并发请求,那么一定会出现多线程的问题。其实HttpClient提供了创建线程安全对象的API让我们调用:

packagecom.caishaohua.internet.request;

importorg.apache.http.HttpVersion;

importorg.apache.http.client.HttpClient;

importorg.apache.http.conn.ClientConnectionManager;

importorg.apache.http.conn.params.ConnManagerParams;

importorg.apache.http.conn.scheme.PlainSocketFactory;

importorg.apache.http.conn.scheme.Scheme;

importorg.apache.http.conn.scheme.SchemeRegistry;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

importorg.apache.http.params.BasicHttpParams;

importorg.apache.http.params.HttpConnectionParams;

importorg.apache.http.params.HttpParams;

importorg.apache.http.params.HttpProtocolParams;

importorg.apache.http.protocol.HTTP;

importorg.apache.http.protocol.HttpProcessor;

/**

* Created by Administrator on 2016/6/19.

*/

public class SafetyCustomerHttpClient {

         private static final String CHARSET= HTTP.UTF_8;

         private static HttpClient custormerHttpClient;

         private SafetyCustomerHttpClient(){

              }

          public static synchronized  HttpClient getHttpClient(){

              if(null==custormerHttpClient){

                 HttpParams httpParams =new BasicHttpParams();

                 //设置基本参数

                  HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

                  HttpProtocolParams.setContentCharset(httpParams,CHARSET);

                   HttpProtocolParams.setUseExpectContinue(httpParams,true);

                   HttpProtocolParams.setUserAgent(httpParams,""+

                              "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");

                  //超时设置

                  //从连接池获取连接的超时时间

                   ConnManagerParams.setTimeout(httpParams,8000);

                 //连接超时

                  HttpConnectionParams.setConnectionTimeout(httpParams,8000);

                 //请求超时

                   HttpConnectionParams.setSoTimeout(httpParams,8000);

                   //这种HttpClient支持http和https两种模式

                    SchemeRegistry  schemeRegistry =new SchemeRegistry();

                    schemeRegistry.register(newScheme("http", PlainSocketFactory.getSocketFactory(),80));

                   schemeRegistry.register(newScheme("https",PlainSocketFactory.getSocketFactory(),443));

                  //使用线程安全的连接管理来创建HttpClient

                 ClientConnectionManager connectionManager =new  ThreadSafeClientConnManager(httpParams,schemeRegistry);

                 custormerHttpClient=new DefaultHttpClient(connectionManager,httpParams);

            }

            returncustormerHttpClient;

          }

}

在代码中,通过getHttpClient方法给HttpClient配置基本的设置,然后在使用ThreadSafeClientConnManager()来创建HttpClient。

上一篇下一篇

猜你喜欢

热点阅读