安卓 okhttp小結

來源:互聯網
上載者:User

標籤:type   jar   success   代碼   exec   llb   cli   syn   exe   

概念及原理

特性

    支援同步阻塞調用和具有回調的非同步呼叫。

    OkHttp支援Android 2.3及以上版本。 對於JDK,最低要求是1.7。

 

Request是OkHttp中訪問的請求,Builder是輔助類。Response即OkHttp中的響應。

 

代碼部分

項目中使用EventBus:

S1.添加jar包或依賴

    compile ‘com.squareup.okhttp3:okhttp:3.8.0‘

    compile ‘com.squareup.okio:okio:1.13.0‘

S2.建立POST/GET請求:

   

S3.代碼混淆

    -dontwarn okio.**

-dontwarn javax.annotation.Nullable

-dontwarn javax.annotation.ParametersAreNonnullByDefault

OKHTTP主要方法:

// 建立請求MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8");Request request = new Request.Builder()                        .post(RequestBody.create(MEDIA_TYPE, json))                        .url(url)                        .build();// 建立響應Response response = client.newCall(request).execute();  // syncResponse response = client.newCall(request).enqueue(new Callback() { // async                   @Override                    public void onFailure(Call call, IOException e) {                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                    }                });// 擷取結果if (response.isSuccessful()) {     response.body().string();}// 逾時設定// 提交JSON資料樣本    public static void postJsonExamAsync() {        String url = "http:localhost:8080/login/commitUserInfo";        String postJson = "{}";        RequestBody body = RequestBody.create(MEDIA_TYPE, postJson);        Request request = new Request.Builder()                .url(url)                .post(body)                .build();        client.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                e.printStackTrace();            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);                Headers responseHeaders = response.headers();                for (int i = 0; i < responseHeaders.size(); i++) {                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));                }                System.out.println(response.body().string());            }        });    }

  

參考文檔:

    Github-okhttp link2

    Github文檔-okio

    Android OkHttp完全解析 是時候來瞭解OkHttp了

    OkHttp使用教程

    

安卓 okhttp小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.