Android okHttp網路請求之Get/Post請求

總李寫程式碼發表於2016-05-25

   前言:

      之前專案中一直使用的Xutils開源框架,從xutils 2.1.5版本使用到最近的xutils 3.0,使用起來也是蠻方便的,只不過最近想著完善一下app中使用的開源框架,由於Xutils裡面包含的東西相對來說比較雜,有資料庫、圖片快取、註解、網路請求等等,秉著一個開源庫只處理一件事的想法,決定逐步替換到Xutils,上網搜了一下比較好的開源框架,就找到了okHttp、volley、android-async-http等比較推薦的開源網路請求,該如何選擇呢?

     okHttp相關文章地址:

   okHttp、volley、android-async-http對比:

  • volley是一個簡單的非同步http庫,僅此而已。缺點是不支援同步,這點會限制開發模式;不能post大資料,所以不適合用來上傳檔案
  • android-async-http。與volley一樣是非同步網路庫,但volley是封裝的httpUrlConnection,它是封裝的httpClient,而android平臺不推薦用HttpClient了,所以這個庫已經不適合android平臺了。
  • okhttp是高效能的http庫,支援同步、非同步,而且實現了spdy、http2、websocket協議,api很簡潔易用,和volley一樣實現了http協議的快取。

   okHttp介紹:

      通過上面的對比說明,讓你不得不做出明智的選擇,OkHttp是一個相對成熟的解決方案,據說Android4.4的原始碼中可以看到HttpURLConnection已經替換成OkHttp實現了,所以決定選擇採用okhttp。

      官網地址:http://square.github.io/okhttp/

      官方API地址:http://m.blog.csdn.net/article/details?id=50747352

      github原始碼地址:https://github.com/square/okhttp

   okHttp主要類:

       1.)OkHttpClient.java

       2.)Request.java

       3.)Call.java

       4.)RequestBody.java

       5.)Response.java

   okHttp使用:

  1.)新增引用 build.gradle新增如下    
compile 'com.squareup.okhttp3:okhttp:3.2.0'
 2.)建立一個RequestManager類接下來以專案中用來的實戰為例 

     RequestManager.java 全域性屬性解說

    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");//mdiatype 這個需要和服務端保持一致
    private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");//mdiatype 這個需要和服務端保持一致
    private static final String TAG = RequestManager.class.getSimpleName();
    private static final String BASE_URL = "http://xxx.com/openapi";//請求介面根地址
    private static volatile RequestManager mInstance;//單利引用
    public static final int TYPE_GET = 0;//get請求
    public static final int TYPE_POST_JSON = 1;//post請求引數為json
    public static final int TYPE_POST_FORM = 2;//post請求引數為表單
    private OkHttpClient mOkHttpClient;//okHttpClient 例項
    private Handler okHttpHandler;//全域性處理子執行緒和M主執行緒通訊

     RequestManager.java 建構函式

   /**
     * 初始化RequestManager
     */
    public RequestManager(Context context) {
        //初始化OkHttpClient
        mOkHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)//設定超時時間
                .readTimeout(10, TimeUnit.SECONDS)//設定讀取超時時間
                .writeTimeout(10, TimeUnit.SECONDS)//設定寫入超時時間
                .build();
        //初始化Handler
        okHttpHandler = new Handler(context.getMainLooper());
    }

   RequestManager.java 獲取單利引用 這裡用到了雙重檢查鎖實現單例

   /**
     * 獲取單例引用
     *
     * @return
     */
    public static RequestManager getInstance(Context context) {
        RequestManager inst = mInstance;
        if (inst == null) {
            synchronized (RequestManager.class) {
                inst = mInstance;
                if (inst == null) {
                    inst = new RequestManager(context.getApplicationContext());
                    mInstance = inst;
                }
            }
        }
        return inst;
    }
 3.)實現okHttp同步請求

     同步請求統一入口

   /**
     *  okHttp同步請求統一入口
     * @param actionUrl  介面地址
     * @param requestType 請求型別
     * @param paramsMap   請求引數
     */
    public void requestSyn(String actionUrl, int requestType, HashMap<String, String> paramsMap) {
        switch (requestType) {
            case TYPE_GET:
                requestGetBySyn(actionUrl, paramsMap);
                break;
            case TYPE_POST_JSON:
                requestPostBySyn(actionUrl, paramsMap);
                break;
            case TYPE_POST_FORM:
                requestPostBySynWithForm(actionUrl, paramsMap);
                break;
        }
    }
 okHttp get同步請求
    /**
     * okHttp get同步請求
     * @param actionUrl  介面地址
     * @param paramsMap   請求引數
     */
    private void requestGetBySyn(String actionUrl, HashMap<String, String> paramsMap) {
        StringBuilder tempParams = new StringBuilder();
        try {
            //處理引數
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                //對引數進行URLEncoder
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
            //補全請求地址
            String requestUrl = String.format("%s/%s?%s", BASE_URL, actionUrl, tempParams.toString());
            //建立一個請求
            Request request = addHeaders().url(requestUrl).build();
            //建立一個Call
            final Call call = mOkHttpClient.newCall(request);
            //執行請求
            final Response response = call.execute();
            response.body().string();
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
okHttp post同步請求
    /**
     * okHttp post同步請求
     * @param actionUrl  介面地址
     * @param paramsMap   請求引數
     */
    private void requestPostBySyn(String actionUrl, HashMap<String, String> paramsMap) {
        try {
            //處理引數
            StringBuilder tempParams = new StringBuilder();
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
            //補全請求地址
            String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
            //生成引數
            String params = tempParams.toString();
            //建立一個請求實體物件 RequestBody
            RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params);
            //建立一個請求
            final Request request = addHeaders().url(requestUrl).post(body).build();
            //建立一個Call
            final Call call = mOkHttpClient.newCall(request);
            //執行請求
            Response response = call.execute();
            //請求執行成功
            if (response.isSuccessful()) {
                //獲取返回資料 可以是String,bytes ,byteStream
                Log.e(TAG, "response ----->" + response.body().string());
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
okHttp post同步請求表單提交
 /**
     * okHttp post同步請求表單提交
     * @param actionUrl 介面地址
     * @param paramsMap 請求引數
     */
    private void requestPostBySynWithForm(String actionUrl, HashMap<String, String> paramsMap) {
        try {
            //建立一個FormBody.Builder
            FormBody.Builder builder = new FormBody.Builder();
            for (String key : paramsMap.keySet()) {
                //追加表單資訊
                builder.add(key, paramsMap.get(key));
            }
            //生成表單實體物件
            RequestBody formBody = builder.build();
            //補全請求地址
            String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
            //建立一個請求
            final Request request = addHeaders().url(requestUrl).post(formBody).build();
            //建立一個Call
            final Call call = mOkHttpClient.newCall(request);
            //執行請求
            Response response = call.execute();
            if (response.isSuccessful()) {
                Log.e(TAG, "response ----->" + response.body().string());
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
 4.)實現okHttp非同步請求

   非同步請求統一入口

  /**
     * okHttp非同步請求統一入口
     * @param actionUrl   介面地址
     * @param requestType 請求型別
     * @param paramsMap   請求引數
     * @param callBack 請求返回資料回撥
     * @param <T> 資料泛型
     **/
    public <T> Call requestAsyn(String actionUrl, int requestType, HashMap<String, String> paramsMap, ReqCallBack<T> callBack) {
        Call call = null;
        switch (requestType) {
            case TYPE_GET:
                call = requestGetByAsyn(actionUrl, paramsMap, callBack);
                break;
            case TYPE_POST_JSON:
                call = requestPostByAsyn(actionUrl, paramsMap, callBack);
                break;
            case TYPE_POST_FORM:
                call = requestPostByAsynWithForm(actionUrl, paramsMap, callBack);
                break;
        }
        return call;
    }
okHttp get非同步請求
  /**
     * okHttp get非同步請求
     * @param actionUrl 介面地址
     * @param paramsMap 請求引數
     * @param callBack 請求返回資料回撥
     * @param <T> 資料泛型
     * @return
     */
    private <T> Call requestGetByAsyn(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) {
        StringBuilder tempParams = new StringBuilder();
        try {
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
            String requestUrl = String.format("%s/%s?%s", BASE_URL, actionUrl, tempParams.toString());
            final Request request = addHeaders().url(requestUrl).build();
            final Call call = mOkHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    failedCallBack("訪問失敗", callBack);
                    Log.e(TAG, e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.e(TAG, "response ----->" + string);
                        successCallBack((T) string, callBack);
                    } else {
                        failedCallBack("伺服器錯誤", callBack);
                    }
                }
            });
            return call;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }
okHttp post非同步請求
  /**
     * okHttp post非同步請求
     * @param actionUrl 介面地址
     * @param paramsMap 請求引數
     * @param callBack 請求返回資料回撥
     * @param <T> 資料泛型
     * @return
     */
    private <T> Call requestPostByAsyn(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) {
        try {
            StringBuilder tempParams = new StringBuilder();
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
            String params = tempParams.toString();
            RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params);
            String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
            final Request request = addHeaders().url(requestUrl).post(body).build();
            final Call call = mOkHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    failedCallBack("訪問失敗", callBack);
                    Log.e(TAG, e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.e(TAG, "response ----->" + string);
                        successCallBack((T) string, callBack);
                    } else {
                        failedCallBack("伺服器錯誤", callBack);
                    }
                }
            });
            return call;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }
okHttp post非同步請求表單提交
 /**
     * okHttp post非同步請求表單提交
     * @param actionUrl 介面地址
     * @param paramsMap 請求引數
     * @param callBack 請求返回資料回撥
     * @param <T> 資料泛型
     * @return
     */
    private <T> Call requestPostByAsynWithForm(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) {
        try {
            FormBody.Builder builder = new FormBody.Builder();
            for (String key : paramsMap.keySet()) {
                builder.add(key, paramsMap.get(key));
            }
            RequestBody formBody = builder.build();
            String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
            final Request request = addHeaders().url(requestUrl).post(formBody).build();
            final Call call = mOkHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    failedCallBack("訪問失敗", callBack);
                    Log.e(TAG, e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.e(TAG, "response ----->" + string);
                        successCallBack((T) string, callBack);
                    } else {
                        failedCallBack("伺服器錯誤", callBack);
                    }
                }
            });
            return call;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }

介面ReqCallBack.java實現

public interface ReqCallBack<T> {
    /**
     * 響應成功
     */
     void onReqSuccess(T result);

    /**
     * 響應失敗
     */
     void onReqFailed(String errorMsg);
}
5.)如何新增請求頭
   /**
     * 統一為請求新增頭資訊
     * @return
     */
    private Request.Builder addHeaders() {
        Request.Builder builder = new Request.Builder()
                .addHeader("Connection", "keep-alive")
                .addHeader("platform", "2")
                .addHeader("phoneModel", Build.MODEL)
                .addHeader("systemVersion", Build.VERSION.RELEASE)
                .addHeader("appVersion", "3.2.0");
        return builder;
    }

 6.)成功與失敗 回撥處理

   成功回撥處理

 /**
     * 統一同意處理成功資訊
     * @param result
     * @param callBack
     * @param <T>
     */
    private <T> void successCallBack(final T result, final ReqCallBack<T> callBack) {
        okHttpHandler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.onReqSuccess(result);
                }
            }
        });
    }

失敗回撥處理

 /**
     * 統一處理失敗資訊
     * @param errorMsg
     * @param callBack
     * @param <T>
     */
    private <T> void failedCallBack(final String errorMsg, final ReqCallBack<T> callBack) {
        okHttpHandler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.onReqFailed(errorMsg);
                }
            }
        });
    }

小結:基於上述基本上可以實現http之間的網路通訊,接下來我們來研究如何搞定檔案的上傳和下載。具體實現參考(http://www.cnblogs.com/whoislcj/p/5529827.html)

 

   

        

 

相關文章