Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用OKHttp包處理HTTP相關操作的基本用法講解

Android使用OKHttp包處理HTTP相關操作的基本用法講解

編輯:關於Android編程

OKHttp是一款高效的HTTP客戶端,支持連接同一地址的鏈接共享同一個socket,通過連接池來減小響應延遲,還有透明的GZIP壓縮,請求緩存等優勢。(GitHub頁:https://github.com/square/okhttp)

Android為我們提供了兩種HTTP交互的方式:HttpURLConnection 和 Apache HTTP Client,雖然兩者都支持HTTPS,流的上傳和下載,配置超時,IPv6和連接池,已足夠滿足我們各種HTTP請求的需求。但更高效的使用HTTP 可以讓您的應用運行更快、更節省流量。而OkHttp庫就是為此而生。
OkHttp是一個高效的HTTP庫:

  • 支持 SPDY ,共享同一個Socket來處理同一個服務器的所有請求
  • 如果SPDY不可用,則通過連接池來減少請求延時
  • 無縫的支持GZIP來減少數據流量
  • 緩存響應數據來減少重復的網絡請求
  • 會從很多常用的連接問題中自動恢復。

如果您的服務器配置了多個IP地址,當第一個IP連接失敗的時候,OkHttp會自動嘗試下一個IP。OkHttp還處理了代理服務器問題和SSL握手失敗問題。
使用 OkHttp 無需重寫您程序中的網絡代碼。OkHttp實現了幾乎和java.net.HttpURLConnection一樣的API。如果您用了 Apache HttpClient,則OkHttp也提供了一個對應的okhttp-apache 模塊。


引入
可以通過下載jar包直接導入工程地址
或者通過構建的方式導入
MAVEN:

<dependency>
 <groupId>com.squareup.okhttp</groupId>
 <artifactId>okhttp</artifactId>
 <version>2.4.0</version>
</dependency>

GRADLE:

compile 'com.squareup.okhttp:okhttp:2.4.0'

用法
在向網絡發起請求的時候,我們最常用的就是GET和POST,下面就來看看如何使用
1. GET
在OKHttp,每次網絡請求就是一個Request,我們在Request裡填寫我們需要的url,header等其他參數,再通過Request構造出Call,Call內部去請求參數,得到回復,並將結果告訴調用者。
package com.jackchan.test.okhttptest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.File;
import java.io.IOException;
public class TestActivity extends ActionBarActivity {
  private final static String TAG = "TestActivity";
  private final OkHttpClient client = new OkHttpClient();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          execute();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }).start();
  }
  public void execute() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    Response response = client.newCall(request).execute();
    if(response.isSuccessful()){
      System.out.println(response.code());
      System.out.println(response.body().string());
    }
  }
}
我們通過Request.Builder傳入url,然後直接execute執行得到Response,通過Response可以得到code,message等信息。
這個是通過同步的方式去操作網絡請求,而android本身是不允許在UI線程做網絡請求操作的,因此我們需要自己開啟一個線程。
當然,OKHttp也支持異步線程並且有回調返回,有了上面同步的基礎,異步只要稍加改動即可
private void enqueue(){
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    client.newCall(request).enqueue(new Callback() {
      @Override
      public void onFailure(Request request, IOException e) {
      }
      @Override
      public void onResponse(Response response) throws IOException {
        //NOT UI Thread
        if(response.isSuccessful()){
          System.out.println(response.code());
          System.out.println(response.body().string());
        }
      }
    });
  }
就是在同步的基礎上講execute改成enqueue,並且傳入回調接口,但接口回調回來的代碼是在非UI線程的,因此如果有更新UI的操作記得用Handler或者其他方式。
2、POST
說完GET該介紹些如何使用POST,POST情況下我們一般需要傳入參數,甚至一些header,傳入參數或者header
比如傳入header
Request request = new Request.Builder() 
.url("https://api.github.com/repos/square/okhttp/issues") 
.header("User-Agent", "OkHttp Headers.java") 
.addHeader("Accept", "application/json; q=0.5") 
.addHeader("Accept", "application/vnd.github.v3+json") 
.build(); 
傳入POST參數
RequestBody formBody = new FormEncodingBuilder()
  .add("platform", "android")
  .add("name", "bug")
  .add("subject", "XXXXXXXXXXXXXXX")
  .build();
  Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
可以看出來,傳入header或者post參數都是傳到Request裡面,因此最後的調用方式也和GET方式一樣
Response response = client.newCall(request).execute();
  if (response.isSuccessful()) {
    return response.body().string();
  } else {
    throw new IOException("Unexpected code " + response);
  }
這個代碼是同步網絡請求,異步就改成enqueue就行了。
請求緩存
在網絡請求中,緩存技術是一項應用比較廣泛的技術,需要對請求過的網絡資源進行緩存,而okhttp也支持這一技術,也使用十分方便,前文漲經常出現的OkHttpclient這個時候就要派送用場了。看下面代碼
package com.jackchan.test.okhttptest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.CacheControl;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.File;
import java.io.IOException;
public class TestActivity extends ActionBarActivity {
  private final static String TAG = "TestActivity";
  private final OkHttpClient client = new OkHttpClient();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    File sdcache = getExternalCacheDir();
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    client.setCache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          execute();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }).start();
  }
  public void execute() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    Response response1 = client.newCall(request).execute();
    if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);
    String response1Body = response1.body().string();
    System.out.println("Response 1 response:     " + response1);
    System.out.println("Response 1 cache response:  " + response1.cacheResponse());
    System.out.println("Response 1 network response: " + response1.networkResponse());
    Response response2 = client.newCall(request).execute();
    if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);
    String response2Body = response2.body().string();
    System.out.println("Response 2 response:     " + response2);
    System.out.println("Response 2 cache response:  " + response2.cacheResponse());
    System.out.println("Response 2 network response: " + response2.networkResponse());
    System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
  }
}
okhttpclient有點像Application的概念,統籌著整個okhttp的大功能,通過它設置緩存目錄,我們執行上面的代碼,得到的結果如下
201671395230108.jpg (848×127) response1 的結果在networkresponse,代表是從網絡請求加載過來的,而response2的networkresponse 就為null,而cacheresponse有數據,因為我設置了緩存因此第二次請求時發現緩存裡有就不再去走網絡請求了。
但有時候即使在有緩存的情況下我們依然需要去後台請求最新的資源(比如資源更新了)這個時候可以使用強制走網絡來要求必須請求網絡數據
 public void execute() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    Response response1 = client.newCall(request).execute();
    if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);
    String response1Body = response1.body().string();
    System.out.println("Response 1 response:     " + response1);
    System.out.println("Response 1 cache response:  " + response1.cacheResponse());
    System.out.println("Response 1 network response: " + response1.networkResponse());
    request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
    Response response2 = client.newCall(request).execute();
    if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);
    String response2Body = response2.body().string();
    System.out.println("Response 2 response:     " + response2);
    System.out.println("Response 2 cache response:  " + response2.cacheResponse());
    System.out.println("Response 2 network response: " + response2.networkResponse());
    System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
  }
上面的代碼中
response2對應的request變成
request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
我們看看運行結果
201671395608033.jpg (853×142) response2的cache response為null,network response依然有數據。
同樣的我們可以使用 FORCE_CACHE 強制只要使用緩存的數據,但如果請求必須從網絡獲取才有數據,但又使用了FORCE_CACHE 策略就會返回504錯誤,代碼如下,我們去okhttpclient的緩存,並設置request為FORCE_CACHE
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    File sdcache = getExternalCacheDir();
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    //client.setCache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          execute();
        } catch (Exception e) {
          e.printStackTrace();
          System.out.println(e.getMessage().toString());
        }
      }
    }).start();
  }
  public void execute() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    Response response1 = client.newCall(request).execute();
    if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);
    String response1Body = response1.body().string();
    System.out.println("Response 1 response:     " + response1);
    System.out.println("Response 1 cache response:  " + response1.cacheResponse());
    System.out.println("Response 1 network response: " + response1.networkResponse());
    request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
    Response response2 = client.newCall(request).execute();
    if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);
    String response2Body = response2.body().string();
    System.out.println("Response 2 response:     " + response2);
    System.out.println("Response 2 cache response:  " + response2.cacheResponse());
    System.out.println("Response 2 network response: " + response2.networkResponse());
    System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
  }
201671395741595.jpg (882×84) 取消操作
網絡操作中,經常會使用到對請求的cancel操作,okhttp的也提供了這方面的接口,call的cancel操作。使用Call.cancel()可以立即停止掉一個正在執行的call。如果一個線程正在寫請求或者讀響應,將會引發IOException,同時可以通過Request.Builder.tag(Object tag)給請求設置一個標簽,並使用OkHttpClient.cancel(Object tag)來取消所有帶有這個tag的call。但如果該請求已經在做讀寫操作的時候,cancel是無法成功的,會拋出IOException異常。
public void canceltest() throws Exception {
    Request request = new Request.Builder()
        .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
        .build();
    final long startNanos = System.nanoTime();
    final Call call = client.newCall(request);
    // Schedule a job to cancel the call in 1 second.
    executor.schedule(new Runnable() {
      @Override
      public void run() {
        System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
        call.cancel();
        System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
      }
    }, 1, TimeUnit.SECONDS);
    try {
      System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
      Response response = call.execute();
      System.out.printf("call is cancel:" + call.isCanceled() + "%n");
      System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
          (System.nanoTime() - startNanos) / 1e9f, response);
    } catch (IOException e) {
      System.out.printf("%.2f Call failed as expected: %s%n",
          (System.nanoTime() - startNanos) / 1e9f, e);
    }
  }
成功取消
2016713100051268.jpg (613×103) 取消失敗
2016713100130237.jpg (867×102)
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved