15.經常使用網絡開源庫的特色與簡單用法(轉)

轉載:http://blog.csdn.net/lin_t_s/article/details/61199986html

今天來總結一下經常使用網絡開源庫的特色與簡單用法。java

AsyncHttp 
Asynchttp 基於Apache HttpClient庫;android

  • 特色: 
    1.異步網絡請求,在匿名回調中處理響應,使用簡單 
    2.使用線程池來管理併發的網絡請求 
    3.永久的cookie保存,內部實現用的是Android的SharedPreferences 
    4.庫很小,全部的一切只有60kb 
    5.自動智能的請求重連 
    6.內置Json解析git

  • 用法:github

AsyncHttpClient client = new AsyncHttpClient(); //永久保存cookie PersistentCookieStore myCookieStore = new PersistentCookieStore(this); client.setCookieStore(myCookieStore); client.get(getAbsoluteUrl(url), params, responseHandler); client.post(getAbsoluteUrl(url), params, responseHandler); private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBytes) { } @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { } }; 

Volley 
Volley,谷歌在android2.2以後不建議使用Http Client,Volley在android2.2及如下版本使用Http Client,而android2.3 
及以上版本使用HttpUrlConnection,它是android開發團隊在2013年推出的,大小大概153KB;數據庫

  • 特色: 
    1.特別適合數據量小,通訊頻繁的網絡操做,異步網絡請求 
    2.支持使用okhttp做爲傳輸層 
    3.擴展性強。Volley 中大可能是基於接口的設計,可配置性強 
    4.提供簡便的圖片加載工具 
    5.訪問網絡數據時直接開啓固定個數線程訪問網絡 
    6.不支持 post 大數據,因此不適合上傳文件 
    用法:
//建立一個RequestQueue對象。 RequestQueue mQueue = Volley.newRequestQueue(context); //建立一個Request對象。 StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener) { @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } }; //設置請求時間 stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f)); //將Request對象添加到RequestQueue裏面。 mQueue.add(stringRequest); } { 加載圖片只需把StringRequest改成ImageRequest ,而後設置一些圖片寬高 圖片scaleType屬性等參數 } { 使用ImageLoader能夠實現對圖片的緩存 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);

XUtils 
XUtils 一個 Android 公共庫框架,主要包括四個部分:View,Db, Http, Bitmap 四個模塊,大小900KB左右;json

    • View 模塊主要的功能是經過註解綁定 UI,資源,事件。
    • Db 模塊是一個數據庫 orm 框架, 簡單的語句就能進行數據的操做。
    • Http 模塊主要訪問網絡,支持同步,異步方式的請求,支持文件的下載。
    • Bitmap 模塊是加載圖片以及圖片的處理, 支持加載本地,網絡圖片。並且支持圖片的內存和本地緩存。
  • 特色: 
    1.經過HTTPclient請求網絡,bitmap模塊經過urlhttpconnection 
    2.支持大數據的請求,速度比volley稍快,擴展性低 
    3.經過開啓線程池來管理線程

用法:api

//get請求 HttpUtils util = new HttpUtils(); util.send(HttpRequest.HttpMethod.GET, "http://www.baidu.com", new RequestCallBack<Sting>() { @Override public void onSuccess(ResponseInfo<Sting> responseInfo) { // TODO Auto-generated method stub } @Override public void onFailure(HttpException error, String msg) { // TODO Auto-generated method stub } }); //post請求: HttpUtils httpUtils = new HttpUtils(); RequestParams params = new RequestParams(); params.addBodyParameter("CODE", "31"); httpUtils.send(HttpMethod.POST, Constants.URL_HOST, params, new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { } @Override public void onSuccess(ResponseInfo<String> arg0) { } } });

Okhttp 
Okhttp 是一個 Java 的 HTTP+SPDY(SPDY是基於TCP的應用層協議,用以最小化網絡延遲,提高網絡速度) 客戶端開發包,同時也支持 Android。須要Android 2.3以上。緩存

  • 特色: 
    1.是安卓版http客戶端,很是高效,支持SDPY,GZIP和HTTP緩存 
    2.自動處理常見的網絡問題,好比二次鏈接,SSL握手問題。 
    3.從4.4開始httpurlconnection的底層實現採用的是okhttp 
    4.支持json,經過回調進行異步操做 
    5. 基於 NIO (非阻塞式讀取數據)和 Okio(基於 IO 和 NIO 基礎上作的一個更簡單、高效處理數據流的一個庫)

用法:markdown

//新建一個okhttpclient對象 OkHttpClient client = new OkHttpClient(); //經過Request.Builder方法新建一個request對象 Request request = new Request.Builder() .url(url) .build(); Response response = null; try { //調用request的newCall方法獲得響應結果 response = client.newCall(request).execute(); return response.body().string(); } catch (IOException e) { e.printStackTrace(); } return null;

Retrofit 
Retrofit 支持同步和異步兩種方式,在使用時,須要將請求地址轉換爲接口,經過註解來指定請求方法,請求參數,請求頭,返回值等信息; 
Retrofit+okhttp+rxjava是如今經常使用的請求形式

  • 特色: 
    1.性能最好,處理最快,速度上比volley更快 
    2.傳輸層默認使用okhttp 
    3.默認使用Gson

    用法:

//首先完成請求所用的service,是一個interface,徹底經過註解完成配置 public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); } //建立Retrofit實例 Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); //獲得GitHubService對象 GitHubService service = retrofit.create(GitHubService.class); //調用接口方法 Call<List<Repo>> repos = service.listRepos("octocat"); 
相關文章
相關標籤/搜索