Android 網絡請求庫:okHttp

jopen 10年前發布 | 51K 次閱讀 Android開發 移動開發 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
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函數將其傳入即可

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的支持

  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

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);
            }
        });


    }

項目主頁:http://www.baiduhome.net/lib/view/home/1435381866122

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!