[轉]Android各大網絡請求庫的比較及實戰

本身學習android也有一段時間了,在實際開發中,頻繁的接觸網絡請求,而網絡請求的方式不少,最多見的那麼幾個也就那麼幾個。本篇文章對常見的網絡請求庫進行一個總結。

HttpUrlConnection

最開始學android的時候用的網絡請求是HttpUrlConnection,當時不少東西還不知道,可是在android 2.2及如下版本中HttpUrlConnection存在着一些bug,因此建議在android 2.3之後使用HttpUrlConnection,以前使用HttpClient。php

在Android 2.2版本以前,HttpClient擁有較少的bug,所以使用它是最好的選擇。而在Android 2.3版本及之後,HttpURLConnection則是最佳的選擇。它的API簡單,體積較小,於是很是適用於Android項目。壓縮和緩存機制能夠有效地減小網絡訪問的流量,在提高速度和省電方面也起到了較大的做用。對於新的應用程序應該更加偏向於使用HttpURLConnection,由於在之後的工做當中咱們也會將更多的時間放在優化HttpURLConnection上面。java

特色

  • 比較輕便,靈活,易於擴展
  • 在3.0後以及4.0中都進行了改善,如對HTTPS的支持
  • 在4.0中,還增長了對緩存的支持

用法

  1. 首先咱們須要獲取到一個HttpURLConnection實例,通常須要new出一個URL對象,並傳入目標網絡地址,經過調用openConnection()方法得到HttpURLConnection實例。
  2. 獲得該實例後。咱們須要設置一下http請求的的方法,這裏咱們主要研究get和post,默認是使用get方法。get通常用於從服務器獲取數據,post通常用於向服務器提交數據,設置請求方法使用函數setRequestMethod(「POST」)進行設置。
  3. 此外能夠進行一些請求的限制,好比鏈接超時的時間等,能夠經過setConnectTimeout設置超時時間。
  4. 獲取服務器返回的輸入流,使用getInputStream方法獲取。
  5. 讀取內容並處理
  6. 關閉鏈接,經過調用disconnect方法關閉當前的鏈接。
    關鍵代碼以下
    使用過程當中不要忘記添加權限
1
< uses-permission android:name = "android.permission.INTERNET" />

GETandroid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public String get(String urlPath) {
      HttpURLConnection connection =  null ;
      InputStream is =  null ;
      try {
           URL url =  new URL(urlPath);
           //得到URL對象
           connection = (HttpURLConnection) url.openConnection();
           //得到HttpURLConnection對象
           connection.setRequestMethod( "GET" );
           // 默認爲GET
           connection.setUseCaches( false );
           //不使用緩存
           connection.setConnectTimeout( 10000 );
           //設置超時時間
           connection.setReadTimeout( 10000 );
           //設置讀取超時時間
           connection.setDoInput( true );
           //設置是否從httpUrlConnection讀入,默認狀況下是true;
           if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                //相應碼是否爲200
                is = connection.getInputStream();
                //得到輸入流
                BufferedReader reader =  new BufferedReader( new InputStreamReader(is));
                //包裝字節流爲字符流
                StringBuilder response =  new StringBuilder();
                String line;
                while ((line = reader.readLine()) !=  null ) {
                      response.append(line);
                }
                return response.toString();
           }
      catch (Exception e) {
           e.printStackTrace();
      finally {
           if (connection !=  null ) {
                connection.disconnect();
                connection =  null ;
           }
           if (is !=  null ) {
                try {
                     is.close();
                     is =  null ;
                catch (IOException e) {
                     e.printStackTrace();
                }
           }
     }
     return null ;
}

POSTgit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
private String post(String urlPath, Map<String, String> params) {
      if (params ==  null || params.size() ==  0 ) {
           return get(urlPath);
      }
      OutputStream os =  null ;
      InputStream is =  null ;
      HttpURLConnection connection =  null ;
      StringBuffer body = getParamString(params);
      byte [] data = body.toString().getBytes();
      try {
            URL url =  new URL(urlPath);
            //得到URL對象
            connection = (HttpURLConnection) url.openConnection();
            //得到HttpURLConnection對象
            connection.setRequestMethod( "POST" );
            // 設置請求方法爲post
            connection.setUseCaches( false );
            //不使用緩存
            connection.setConnectTimeout( 10000 );
            //設置超時時間
            connection.setReadTimeout( 10000 );
            //設置讀取超時時間
            connection.setDoInput( true );
            //設置是否從httpUrlConnection讀入,默認狀況下是true;
            connection.setDoOutput( true );
            //設置爲true後才能寫入參數
            connection.setRequestProperty( "Content-Type" "application/x-www-form-urlencoded" );
            connection.setRequestProperty( "Content-Length" , String.valueOf(data.length));
            os = connection.getOutputStream();
            os.write(data);
            //寫入參數
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                 //相應碼是否爲200
                 is = connection.getInputStream();
                 //得到輸入流
                 BufferedReader reader =  new BufferedReader( new InputStreamReader(is));
                 //包裝字節流爲字符流
                 StringBuilder response =  new StringBuilder();
                 String line;
                 while ((line = reader.readLine()) !=  null ) {
                      response.append(line);
                 }
                 return response.toString();
           }
      catch (Exception e) {
           e.printStackTrace();
      finally {
           //關閉
           if (os !=  null ) {
                try {
                      os.close();
                catch (IOException e) {
                      e.printStackTrace();
                }
           }
           if (is !=  null ) {
                try {
                      is.close();
                catch (IOException e) {
                      e.printStackTrace();
                }
           }
           if (connection !=  null ) {
                connection.disconnect();
                connection =  null ;
           }
      }
      return null ;
}
 
private StringBuffer getParamString(Map<String, String> params) {
      StringBuffer result =  new StringBuffer();
      Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
      while (iterator.hasNext()) {
            Map.Entry<String, String> param = iterator.next();
            String key = param.getKey();
            String value = param.getValue();
            result.append(key).append( '=' ).append(value);
            if (iterator.hasNext()) {
                  result.append( '&' );
            }
      }
      return result;
}

以上代碼參考了部分LessCode項目github

HttpClient

特色

  • 高效穩定,可是維護成本高昂,故android 開發團隊不肯意在維護該庫而是轉投更爲輕便的HttpUrlConnection

用法

  1. HttpClient是一個接口,所以沒法直接建立它的實例,通常都是建立一個DefaultHttpClient實例
  2. 若是要發起Get請求,須要建立一個HttpGet對象,並傳入請求地址
  3. 若是要發起Post請求,須要建立一個HttpPost對象。並傳入請求地址,經過setEntity函數設置請求參數
  4. 調用execute方法,傳入HttpGet或者HttpPost實例,執行後返回HttpResponse對象,判斷響應狀態碼
  5. 解析響應結果,經過調用getEntity函數得到一個HttpEntity對象,以後能夠經過EntityUtils.toString方法將其轉換爲字符串

因爲在android2.3以後就被HttpUrlConnection取代了,這裏也不過多介紹了,不過當初學習它的時候還沒接觸到其餘庫,就感受它好方便,下面簡單貼出使用方法數據庫

GETjson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private String get(String url){
       HttpClient client= null ;
       HttpGet request= null ;
       try {
            client= new DefaultHttpClient();
            request= new HttpGet(url);
            HttpResponse response=client.execute(request);
            if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                  String result=EntityUtils.toString(response.getEntity(), "UTF-8" );
                  return result;
            }
       catch (IOException e) {
            e.printStackTrace();
       }
       return null ;
}

POST緩存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private String post(String url,List<NameValuePair> params){
       HttpClient client= null ;
       HttpPost request= null ;
       try {
            client= new DefaultHttpClient();
            request= new HttpPost(url);
            request.setEntity( new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse response=client.execute(request);
            if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                  String result=EntityUtils.toString(response.getEntity(), "UTF-8" );
                  return result;
            }
       catch (IOException e) {
            e.printStackTrace();
       }
       return null ;
  }

以上代碼參考了郭霖《第一行代碼》——HttpClient部分服務器

Android Asynchronous Http Client

Android Asynchronous Http Client一看名字就知道它是基於Http Client的,可是呢在安卓中Http Client已經廢棄了,因此也不建議使用這個庫了。而後仍然有一些可取的內容值得學習,因此這裏也介紹一下。cookie

特色

  • 因此請求在子線程中完成,請求回調在調用該請求的線程中完成
  • 使用線程池
  • 使用RequestParams類封裝請求參數
  • 支持文件上傳
  • 持久化cookie到SharedPreferences,我的感受這一點也是這個庫的重要特色,能夠很方便的完成一些模擬登陸
  • 支持json
  • 支持HTTP Basic Auth

用法

編寫一個靜態的HttpClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.edu.zafu.http;
 
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
  public class TestClient {
       private static final String BASE_URL =  "http://121.41.119.107/" ;
 
       private static AsyncHttpClient client =  new AsyncHttpClient();
 
       public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
             client.get(getAbsoluteUrl(url), params, responseHandler);
       }
 
       public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
             client.post(getAbsoluteUrl(url), params, responseHandler);
       }
 
       private static String getAbsoluteUrl(String relativeUrl) {
             return BASE_URL + relativeUrl;
       }
}

調用get或者post方法

參數經過RequestParams傳遞,沒有參數則傳遞null

1
2
RequestParams  params =  new RequestParams();
params.put( "" , "" );

若是要保存cookie,在發起請求以前調用如下代碼

1
2
PersistentCookieStore myCookieStore =  new PersistentCookieStore( this );
client.setCookieStore(myCookieStore);

以後請求所獲得的cookie都會自動持久化

若是要本身添加cookie,則調用如下代碼

1
2
3
4
5
BasicClientCookie newCookie =  new BasicClientCookie( "cookiesare" "awesome" );
newCookie.setVersion( 1 );
newCookie.setDomain( "mydomain.com" );
newCookie.setPath( "/" );
myCookieStore.addCookie(newCookie);

使用
在回調函數中處理返回結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private void get(){
         TestClient.get( "test/index.php" null new AsyncHttpResponseHandler() {
             @Override
             public void onSuccess( int statusCode, Header[] headers,  byte [] responseBody) {
 
             }
 
             @Override
             public void onFailure( int statusCode, Header[] headers,  byte [] responseBody, Throwable error) {
 
             }
         });
     }
     private void post(){
         RequestParams params =  new RequestParams();
         params.put( "user" , "asas" );
         params.put( "pass" , "12121" );
         params.put( "time" , "1212121" );
         TestClient.post( "test/login.php" , params,  new AsyncHttpResponseHandler() {
             @Override
             public void onSuccess( int statusCode, Header[] headers,  byte [] responseBody) {
 
             }
 
             @Override
             public void onFailure( int statusCode, Header[] headers,  byte [] responseBody, Throwable error) {
 
             }
         });
     }

以上代碼參考了Android Asynchronous Http Client官方實例

Volley

既然在android2.2以後不建議使用Http Client,那麼有沒有一個庫是android2.2及如下版本使用Http Client,而android2.3及以上版本使用HttpUrlConnection的呢,答案是確定的,就是Volley,它是android開發團隊在2013年Google I/O大會上推出了一個新的網絡通訊框架

Volley能夠說是把AsyncHttpClient和Universal-Image-Loader的優勢集於了一身,既能夠像AsyncHttpClient同樣很是簡單地進行HTTP通訊,也能夠像Universal-Image-Loader同樣輕鬆加載網絡上的圖片。除了簡單易用以外,Volley在性能方面也進行了大幅度的調整,它的設計目標就是很是適合去進行數據量不大,但通訊頻繁的網絡操做,而對於大數據量的網絡操做,好比說下載文件等,Volley的表現就會很是糟糕

特色

  1. Volley的優點在於處理小文件的http請求;
  2. 在Volley中也是可使用Okhttp做爲傳輸層
  3. Volley在處理高分辨率的圖像壓縮上有很好的支持;
  4. NetworkImageView在GC的使用模式上更加保守,在請求清理上也更加積極,networkimageview僅僅依賴於強大的內存引用,並當一個新請求是來自ImageView或ImageView離開屏幕時 會清理掉全部的請求數據。

用法

  1. 建立一個RequestQueue對象。
  2. 建立一個Request對象。
  3. 將Request對象添加到RequestQueue裏面。

下面一步一步來學習其用法

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void get(){
        RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
        String url= "http://121.41.119.107/test/index.php" ;
        StringRequest request= new StringRequest(url,  new Response.Listener&lt;String&gt;() {
            @Override
            public void onResponse(String response) {
                Log.d( "TAG" ,response);
            }
        },  new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        });
        queue.add(request);
    }

POST
經過指定請求方法爲Request.Method.POST使其成爲post請求,而後從新getParams方法設置請求參數。當發出POST請求的時候,Volley會嘗試調用StringRequest的父類——Request中的getParams()方法來獲取POST參數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private void post() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "http://121.41.119.107/test/login.php" ;
        StringRequest request =  new StringRequest(Request.Method.POST, url,  new Response.Listener&lt;String&gt;() {
            @Override
            public void onResponse(String response) {
                Log.d( "TAG" , response);
            }
        },  new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        }) {
            //重寫getParams方法設置參數
            @Override
            protected Map&lt;String, String&gt; getParams()  throws AuthFailureError {
                Map&lt;String, String&gt; params =  new HashMap&lt;String, String&gt;();
                params.put( "user" "asas" );
                params.put( "pass" "12121" );
                params.put( "time" "1212121" );
                return params;
            }
        };
        queue.add(request);
    }

加載圖片
加載圖像的方法和前面相似,只不過不在是StringRequest而是ImageRequest。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void getImage() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "https://www.baidu.com/img/bdlogo.png" ;
        //第三第四個參數分別用於指定容許圖片最大的寬度和高度,若是指定的網絡圖片的寬度或高度大於這裏的最大值,則會對圖片進行壓縮,指定成0的話就表示無論圖片有多大,都不會進行壓縮。
        //第五個參數就是ImageView裏中的屬性ScaleType
        //第六個參數用於指定圖片的顏色屬性
        ImageRequest request =  new ImageRequest(url,  new Response.Listener&lt;Bitmap&gt;() {
            @Override
            public void onResponse(Bitmap response) {
                ImageView iv= (ImageView) findViewById(R.id.iv);
                iv.setImageBitmap(response);
            }
        },  0 0 , ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888,  new Response.ErrorListener() {
 
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        });
        queue.add(request);
    }

其實加載圖片的功能還遠遠不止這些,使用ImageLoader能夠實現對圖片的緩存,還能夠過濾重複連接,避免發送重複的請求
ImageLoader的使用方法歸納爲如下幾步
1. 建立一個RequestQueue對象。
2. 建立一個ImageLoader對象。
3. 獲取一個ImageListener對象。
4. 調用ImageLoader的get()方法加載網絡上的圖片。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//繼承ImageCache,使用LruCache實現緩存
    public class BitmapCache  implements ImageLoader.ImageCache {
        private LruCache&lt;String, Bitmap&gt; mCache;
        public BitmapCache() {
            int maxSize =  10 1024 1024 ;
            mCache =  new LruCache&lt;String, Bitmap&gt;(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }
        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
 
    }
    private void getImageByImageLoader() {
        ImageView iv= (ImageView) findViewById(R.id.iv);
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "https://www.baidu.com/img/bdlogo.png" ;
        ImageLoader loader= new ImageLoader(queue, new BitmapCache() );
        // 第一個參數指定用於顯示圖片的ImageView控件
        // 第二個參數指定加載圖片的過程當中顯示的圖片
        // 第三個參數指定加載圖片失敗的狀況下顯示的圖片
        ImageLoader.ImageListener listener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
        // 調用ImageLoader的get()方法來加載圖片
        // 第一個參數就是圖片的URL地址
        // 第二個參數則是剛剛獲取到的ImageListener對象
        // 若是想對圖片的大小進行限制,也可使用get()方法的重載,指定圖片容許的最大寬度和高度,即經過第三第四個參數指定
        loader.get(url,listener);
    }

最後,Volley提供了一種自定義ImageView來加載圖片,其使用方法可歸納爲
1. 建立一個RequestQueue對象。
2. 建立一個ImageLoader對象。
3. 在佈局文件中添加一個NetworkImageView控件。
4. 在代碼中獲取該控件的實例。
5. 設置要加載的圖片地址。

咱們在佈局中申明該控件

1
2
3
4
5
6
< com.android.volley.toolbox.NetworkImageView
         android:id = "@+id/network_image_view"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_centerInParent = "true"
         />

在程序中實現加載

1
2
3
4
5
6
7
8
public void networkImageView(){
         RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
         ImageLoader loader= new ImageLoader(queue, new BitmapCache() );
         NetworkImageView niv= (NetworkImageView) findViewById(R.id.network_image_view);
         niv.setDefaultImageResId(R.mipmap.ic_launcher); //設置加載中顯示的圖片
         niv.setErrorImageResId(R.mipmap.ic_launcher); //設置加載失敗時顯示的圖片
         niv.setImageUrl( "https://www.baidu.com/img/bdlogo.png" ,  loader);//設置目標圖片的URL地址
     }

自定義Request

在實際應用中,每每須要將http請求與json進行集成,而Volley正偏偏支持這樣的方式,不過須要咱們本身自定義Request,這裏咱們使用google的Gson庫進行集成。
1. 繼承Request類
2. 重寫parseNetworkResponse,實現json與實體類轉換,因爲實體類未定,因此採用泛型

下文用到的json字符串以下

1
{ "name" : "lizhangqu" , "age" : 16 }
  • 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.edu.zafu.http;
 
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
 
import java.io.UnsupportedEncodingException;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
public class GsonRequest&lt;T&gt;  extends Request&lt;T&gt; {
 
     private final Response.Listener&lt;T&gt; mListener;
 
     private Gson mGson;
 
     private Class&lt;T&gt; mClass;
 
     public GsonRequest( int method, String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,
                        Response.ErrorListener errorListener) {
         super (method, url, errorListener);
         mGson =  new Gson();
         mClass = clazz;
         mListener = listener;
     }
 
     public GsonRequest(String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,
                        Response.ErrorListener errorListener) {
         this (Method.GET, url, clazz, listener, errorListener);
     }
 
     @Override
     protected Response&lt;T&gt; parseNetworkResponse(NetworkResponse response) {
         try {
             String jsonString =  new String(response.data,
                     HttpHeaderParser.parseCharset(response.headers));
             return Response.success(mGson.fromJson(jsonString, mClass),
                     HttpHeaderParser.parseCacheHeaders(response));
         catch (UnsupportedEncodingException e) {
             return Response.error( new ParseError(e));
         }
     }
 
     @Override
     protected void deliverResponse(T response) {
         mListener.onResponse(response);
     }
 
}

編寫測試實體類,兩個字段一個name一個age

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.edu.zafu.http;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
public class Person {
     private String name;
     private int age;
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this .name = name;
     }
 
     public int getAge() {
         return age;
     }
 
     public void setAge( int age) {
         this .age = age;
     }
 
     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\ '' +
                 ", age=" + age +
                 '}' ;
     }
}

調用方法和StringRequest是同樣的。以下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void json(){
       RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
       String url =  "http://121.41.119.107/test/index.php" ;
       GsonRequest&lt;Person&gt; request= new GsonRequest&lt;Person&gt;(url, Person. class new Response.Listener&lt;Person&gt;() {
           @Override
           public void onResponse(Person response) {
               Log.d( "TAG" ,response.toString());
           }
       },  new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
 
           }
       });
       queue.add(request);
   }

以上代碼參考了郭霖三篇Volley博客文章,分別爲
Android Volley徹底解析(一),初識Volley的基本用法
Android Volley徹底解析(二),使用Volley加載網絡圖片
Android Volley徹底解析(三),定製本身的Request

okHttp

okhttp 是一個 Java 的 HTTP+SPDY 客戶端開發包,同時也支持 Android。須要Android 2.3以上。

特色

  • OKHttp是Android版Http客戶端。很是高效,支持SPDY、鏈接池、GZIP和 HTTP 緩存。
  • 默認狀況下,OKHttp會自動處理常見的網絡問題,像二次鏈接、SSL的握手問題。
  • 若是你的應用程序中集成了OKHttp,Retrofit默認會使用OKHttp處理其餘網絡層請求。
  • 從Android4.4開始HttpURLConnection的底層實現採用的是okHttp.

用法

  1. 新建一個OkHttpClient對象
  2. 經過Request.Builder對象新建一個Request對象
  3. 返回執行結果

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private String get(String url) {
         OkHttpClient client =  new OkHttpClient();
         Request request =  new Request.Builder()
                 .url(url)
                 .build();
         Response response =  null ;
         try {
             response = client.newCall(request).execute();
             return response.body().string();
         catch (IOException e) {
             e.printStackTrace();
         }
         return null ;
     }

POST

POST須要使用RequestBody對象,以後再構建Request對象時調用post函數將其傳入便可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private String post(String url) {
         OkHttpClient client =  new OkHttpClient();
 
         RequestBody formBody =  new FormEncodingBuilder()
                 .add( "user" "Jurassic Park" )
                 .add( "pass" "asasa" )
                 .add( "time" "12132" )
                 .build();
         Request request =  new Request.Builder()
                 .url(url)
                 .post(formBody)
                 .build();
         Response response =  null ;
         try {
             response = client.newCall(request).execute();
             return response.body().string();
         catch (IOException e) {
             e.printStackTrace();
         }
         return null ;
     }

此外,post的使用方法還支持文件等操做,具體使用方法有興趣的能夠自行查閱

對Gson的支持

okHttp還自帶了對Gson的支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private Person gson(String url){
       OkHttpClient client =  new OkHttpClient();
       Gson gson =  new Gson();
       Request request =  new Request.Builder()
               .url(url)
               .build();
       Response response =  null ;
       try {
           response = client.newCall(request).execute();
           Person person = gson.fromJson(response.body().charStream(), Person. class );
           return person;
       catch (IOException e) {
           e.printStackTrace();
       }
       return null ;
   }

異步操做

以上的兩個例子必須在子線程中完成,同時okHttp還提供了異步的方法調用,經過使用回調來進行異步調用,而後okHttp的回調依然不在主線程中,所以該回調中不能操做UI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void getAsync(String url) {
        OkHttpClient client =  new OkHttpClient();
        Request request =  new Request.Builder()
                .url(url)
                .build();
        Response response =  null ;
 
        client.newCall(request).enqueue( new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
 
            }
 
            @Override
            public void onResponse(Response response)  throws IOException {
                String result = response.body().string();
                Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
                //不能操做ui,回調依然在子線程
                Log.d( "TAG" , result);
            }
        });
 
    }

okHttp的使用還有不少內容,這裏也不過多介紹,更多內容,參考官方網址

Retrofit

特色

  1. 性能最好,處理最快
  2. 使用REST API時很是方便;
  3. 傳輸層默認就使用OkHttp;
  4. 支持NIO;
  5. 擁有出色的API文檔和社區支持
  6. 速度上比volley更快;
  7. 若是你的應用程序中集成了OKHttp,Retrofit默認會使用OKHttp處理其餘網絡層請求。
  8. 默認使用Gson

使用

Retrofit支持同步和異步兩種方式,在使用時,須要將請求地址轉換爲接口,經過註解來指定請求方法,請求參數,請求頭,返回值等信息。仍是使用以前的person的那段json值,get請求到服務器後從數據庫查詢數據,返回值爲查詢到的數據,post請求向服務器提交一條數據,返回值爲提交的數據。
首先完成請求所用的service,是一個interface,徹底經過註解完成配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.edu.zafu.http;
 
import retrofit.Callback;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
import retrofit.http.Query;
 
/**
  * Created by lizhangqu on 2015/5/11.
  */
public interface PersonService {
     @Headers ({
             "Cache-Control: max-age=640000" ,
             "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
     })
     //經過註解設置請求頭
     @GET ( "/{test}/rest.php" )
     //設置請求方法爲get,相對路徑爲註解內內容,其中{test}會被@Path註解指定內容替換
     Person getPerson( @Path ( "test" ) String dir, @Query ( "name" ) String name);
     //@Query用於指定參數
 
     @FormUrlEncoded
     //urlencode
     @POST ( "/test/rest1.php" )
     //post提交
     Person updatePerson( @Field ( "name" ) String name, @Field ( "age" int age);
     //@Field提交的域
 
     @POST ( "/test/rest1.php" )
     void updatePerson( @Field ( "name" ) String name, @Field ( "age" int age, Callback&lt;Person&gt; callback);
     //異步回調,不能指定返回值
}

GET
使用時,經過RestAdapter的實例得到一個接口的實例,其本質是動態代理,注意含有返回值的方法是同步的,不能UI線程中調用,應該在子線程中完成

1
2
3
4
5
6
RestAdapter restAdapter =  new RestAdapter.Builder()
                         .setEndpoint( "http://121.41.119.107" )
                         .build();
                 PersonService personService=restAdapter.create(PersonService. class );
                 Person person=personService.getPerson( "test" , "zhangsan" );
                 Log.d( "TAG" ,person.toString());

POST

POST的調用同Get,得到adapter後得到一個代理對象,而後經過這個代理對象進行網絡請求

1
2
Person person1=personService.updatePerson( "lizhangqu" 12 );
Log.d( "TAG" ,person1.toString());

異步請求

若是要使用異步請求,須要將接口中的方法返回值修改會void,再加入回調參數Callback,就如PersonService中第三個方法同樣,請求完成後會回調該callback對象的success或者fail方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
RestAdapter restAdapter =  new RestAdapter.Builder()
                 .setEndpoint( "http://121.41.119.107" )
                 .build();
         PersonService personService=restAdapter.create(PersonService. class );
         personService.updatePerson( "lizhangqu" , 23 new Callback&lt;Person&gt;() {
             @Override
             public void success(Person person, Response response) {
                 Log.d( "TAG" , person.toString());
             }
 
             @Override
             public void failure(RetrofitError error) {
 
             }
         });

Retrofit的使用還有不少內容,剩下的就留給各位讀者自行去發現了,而其官網頁提供了及其詳細的說明。下面提供官方網址

retrofit官網示例

這個庫裏面有不少精華的內容,建議各位仔細的閱讀下官方的文檔。

RoboSpice

見以前寫的一篇博文

RoboSpice:android異步網絡庫簡單用法

總結

網絡請求庫多種多樣,最終其本質思想是一致的,要學會融匯貫通,仍是要fucking the source code。因爲本篇文章已通過長,因此圖片的網絡加載準備另開一篇博客進行整理。

源碼下載

源碼下載

相關文章
相關標籤/搜索