Okhttp使用

Okhttp使用java


 

2019-03-31-20:48:45          雲林原創android


 

1、Okhttp介紹:git

一、Okhttp開源庫出自Square公司,是一個優秀的網絡請求框架,它基於httpURLConnectiond的封裝,支持get/post請求,文件的上傳和下載,圖片的加載,讓寫法更加簡單,能夠處理更加複雜的網絡請求。github

二、Okhttp但是實現的功能web

  • get/post同步、異步請求
  • 文件的上傳,下載
  • 圖片的加載
  • 請求回調等

三、Okhttp的特色緩存

  • 響應緩存
  • 支持GZIP壓縮
  • 支持websocket
  • 多IP切換
  • 減小鏈接池請求延時

2、Okhttp請求準備websocket

##一、OkHttp的依賴的引入網絡

Module的gradle中添加框架

--------------------------------------------------------異步

compile 'com.squareup.okhttp3:okhttp:3.5.0'

--------------------------------------------------------

而後同步一下項目便可。

okhttp有本身的官網,可在官網上查詢最新依賴,官網網址:OKHttp官網

##二、OkHttp中添加權限

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

3、OkHttp進行Get請求

Get 請求只須要四步便可完成:

1 . 獲取OkHttpClient對象:

OkHttpClient client = new OkHttpClient();

2 . 構造Request對象:

Request request = new Request.Builder()
                .get()
                .url("https:www.baidu.com")
                .build();

3 . 將Request封裝爲Call:

Call call = client.newCall(request);

4 . 根據須要調用同步或者異步請求方法  

 同步調用
//同步調用,返回Response,
Response response = call.execute();

  異步調用

//異步調用,並設置回調函數
call.enqueue(new Callback() {
      //請求失敗調用
    @Override
    public void onFailure(Call call, IOException e) {
        Toast.makeText(OkHttpActivityDemo.this, "get failed", Toast.LENGTH_SHORT).show();
    }
    //請求成功調用
    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        final String res = response.body().string();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(OkHttpActivityDemo.this, "get success", Toast.LENGTH_SHORT).show();
          } 
     });
    } 
});

注意:

在異步調用時,回調函數是在子線程,不能在子線程更新UI,須要藉助於 runOnUiThread() 方法或者 Handler 來處理

4、OkHttp進行Post請求提交鍵值對:

  Post 請求和進行 Get 請求很相似。

1 、獲取OkHttpClient對象:

 OkHttpClient client = new OkHttpClient();

二、使用FormBody構建包含鍵值對類型參數的請求體:

FormBody formBody = new FormBody.Builder()
                .add("username", "admin")
                .add("password", "123")
                .build();

三、 構建Request,將FormBody做爲Post方法的參數傳入

Request request = new Request.Builder()
                .url("http://www.jianshu.com/")
                .post(formBody)
                .build();

四、將Request封裝爲Call

Call call = client.newCall(request);

5 . 調用請求,重寫回調方法

call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        Toast.makeText(OkHttpActivityDemo.this, "Post Failed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        final String res = response.body().string();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 Toast.makeText(OkHttpActivityDemo.this, "Post success", Toast.LENGTH_SHORT).show();
            }
        });
    }
});

5、後記

以上就是 OkHttp 經常使用get和set請求的總結,當熱還有文件的上傳和下載等。

相關文章
相關標籤/搜索