OKHttp 快速開始
官方網址:https://square.github.io/okhttp/
代碼倉庫:https://github.com/square/okhttp
該庫是一個第三方庫,用於請求網絡,支持同步和異步兩種請求方式
同步請求
get
對於同步請求在請求時需要開啓子線程,請求成功後需要跳轉到UI線程修改UI。
public void getDatasync(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象
Request request = new Request.Builder()
.url("http://www.baidu.com")//請求接口。如果需要傳參拼接到接口後面。
.build();//創建Request 對象
Response response = null;
response = client.newCall(request).execute();//得到Response 對象
if (response.isSuccessful()) {
Log.d("kwwl","response.code()=="+response.code());
Log.d("kwwl","response.message()=="+response.message());
Log.d("kwwl","res=="+response.body().string());
//此時的代碼執行在子線程,修改UI的操作請使用handler跳轉到UI線程。
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
此時打印結果如下:
response.code()==200;
response.message()OK;
res{“code”:200,“message”:success};
注意事項:
Response.code
是http響應行中的code,如果訪問成功則返回200.這個不是服務器設置的,而是http協議中自帶的。res中的code纔是服務器設置的。注意二者的區別。response.body().string()
本質是輸入流的讀操作,所以它還是網絡請求的一部分,所以這行代碼必須放在子線程。response.body().string()
只能調用一次,在第一次時有返回值,第二次再調用時將會返回null。原因是:response.body().string()
的本質是輸入流的讀操作,必須有服務器的輸出流的寫操作時客戶端的讀操作才能得到數據。而服務器的寫操作只執行一次,所以客戶端的讀操作也只能執行一次,第二次將返回null。
異步請求
get
這種方式不用再次開啓子線程,但回調方法是執行在子線程中,所以在更新UI時還要跳轉到UI線程中。
使用示例如下:
private void getDataAsync() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){//回調的方法執行在子線程。
Log.d("kwwl","獲取數據成功了");
Log.d("kwwl","response.code()=="+response.code());
Log.d("kwwl","response.body().string()=="+response.body().string());
}
}
});
}
異步請求的打印結果與注意事項與同步請求時相同。最大的不同點就是異步請求不需要開啓子線程,enqueue方法會自動將網絡請求部分放入子線程中執行。
注意事項:
- 回調接口的onFailure方法和onResponse執行在子線程。
response.body().string()
方法也必須放在子線程中。當執行這行代碼得到結果後,再跳轉到UI線程修改UI。
post
private void postDataWithParame() {
OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象。
FormBody.Builder formBody = new FormBody.Builder();//創建表單請求體
formBody.add("username","zhangsan");//傳遞鍵值對參數
Request request = new Request.Builder()//創建Request 對象。
.url("http://www.baidu.com")
.post(formBody.build())//傳遞請求體
.build();
client.newCall(request).enqueue(new Callback() {。。。});//回調方法的使用與get異步請求相同,此時略。
}
注意:request.post接受的是一個requestbody對象,只要是該對象的子類,都可以作爲參數傳遞,而formbody就是該對象的一個子類
傳參總結:
字符串key value對:
參考上面的表單體
json串:
OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象。 MediaType mediaType = MediaType.parse("application/json; charset=utf-8");//數據類型爲json格式, String jsonStr = "{\"username\":\"lisi\",\"nickname\":\"李四\"}";//json數據. RequestBody body = RequestBody.create(mediaType, josnStr); Request request = new Request.Builder() .url("http://www.baidu.com") .post(body) .build(); client.newCall(request).enqueue(new Callback() {。。。});//此處省略回調方法。
文件對象:
OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象。 MediaType fileType = MediaType.parse("File/*");//數據類型爲json格式, File file = new File("path");//file對象. RequestBody body = RequestBody.create(fileType , file ); Request request = new Request.Builder() .url("http://www.baidu.com") .post(body) .build(); client.newCall(request).enqueue(new Callback() {。。。});//此處省略回調方法。
混合對象:
OkHttpClient client = new OkHttpClient(); MultipartBody multipartBody =new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("groupId",""+groupId)//添加鍵值對參數 .addFormDataPart("title","title") .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("file/*"), file))//添加文件 .build(); final Request request = new Request.Builder() .url(URLContant.CHAT_ROOM_SUBJECT_IMAGE) .post(multipartBody) .build(); client.newCall(request).enqueue(new Callback() {。。。});
注意:addFormDataPart和addPart並無區別,只是多了一層封裝
/** Add a form data part to the body. */ public Builder addFormDataPart(String name, String value) { return addPart(Part.createFormData(name, value)); } /** Add a form data part to the body. */ public Builder addFormDataPart(String name, String filename, RequestBody body) { return addPart(Part.createFormData(name, filename, body)); } public static Part createFormData(String name, String value) { return createFormData(name, null, RequestBody.create(null, value)); } public static Part createFormData(String name, String filename, RequestBody body) { if (name == null) { throw new NullPointerException("name == null"); } StringBuilder disposition = new StringBuilder("form-data; name="); appendQuotedString(disposition, name); if (filename != null) { disposition.append("; filename="); appendQuotedString(disposition, filename); } return create(Headers.of("Content-Disposition", disposition.toString()), body); }
流對象:
RequestBody body = new RequestBody() { @Override public MediaType contentType() { return null; } @Override public void writeTo(BufferedSink sink) throws IOException {//重寫writeTo方法 FileInputStream fio= new FileInputStream(new File("fileName")); byte[] buffer = new byte[1024*8]; if(fio.read(buffer) != -1){ sink.write(buffer); } } }; OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象。 Request request = new Request.Builder() .url("http://www.baidu.com") .post(body) .build(); client.newCall(request).enqueue(new Callback() {。。。});
以上代碼的與衆不同就是body對象,這個body對象重寫了write方法,裏面有個sink對象。這個是OKio包中的輸出流,有write方法。使用這個方法我們可以實現上傳流的功能。
使用RequestBody上傳文件時,並沒有實現斷點續傳的功能。我可以使用這種方法結合RandomAccessFile類實現斷點續傳的功能
設置請求頭
Request request = new Request.Builder()
.url("http://www.baidu.com")
.header("User-Agent", "OkHttp Headers.java")
.addHeader("token", "myToken")
.build();
下載文件
在OKHttp中並沒有提供下載文件的功能,但是在Response中可以獲取流對象,有了流對象我們就可以自己實現文件的下載。代碼如下:
這段代碼寫在回調接口CallBack的onResponse
方法中:
try{
InputStream is = response.body().byteStream();//從服務器得到輸入流對象
long sum = 0;
File dir = new File(mDestFileDir);
if (!dir.exists()){
dir.mkdirs();
}
File file = new File(dir, mdestFileName);//根據目錄和文件名得到file對象
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024*8];
int len = 0;
while ((len = is.read(buf)) != -1){
fos.write(buf, 0, len);
}
fos.flush();
return file;
}
文件下載的另外一個例子:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.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()){
downlodefile(response, Environment.getExternalStorageDirectory().getAbsolutePath(),"text.txt");
}
}
});
private void downlodefile(Response response, String url, String fileName) {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
//文件大小
long total = response.body().contentLength();
File file = new File(url, fileName);
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
// 進度條
// sum += len;
// int progress = (int) (sum * 1.0f / total * 100);
}
fos.flush();
Log.e("xxxxxxxx", "下載成功");
} catch (Exception e) {
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
}
}
封裝
對於OKHttp的封裝首推的就是hongyang大神的OKHttpUtils
攔截器
Interceptors是Okhttp中的攔截器,官方介紹攔截器是一個強大的監聽器,可以重寫,重試請求(calls)詳細瞭解可以看下Okhttp-wiki 之 Interceptors 攔截器,這篇文章可以算是中文版。
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(
interceptors, null, null, null, 0, originalRequest);
return chain.proceed(originalRequest);
}
從這可以發現okhttp在處理網絡響應時採用的是攔截器機制。okhttp用ArrayList對interceptors進行管理,interceptors將依次被調用。
如上圖:
-
橙色框內是okhttp自帶的Interceptors的實現類,它們都是在call.getResponseWithInterceptorChain()中被添加入 InterceptorChain中,實際上這幾個Interceptor都是在okhttp3後才被引入,它們非常重要,負責了重連、組裝請求頭部、讀/寫緩存、建立socket連接、向服務器發送請求/接收響應的全部過程。
-
在okhttp3之前,這些行爲都封裝在HttpEngine類中。okhttp3之後,HttpEngine已經被刪去,取而代之的是這5個Interceptor,可以說一次網絡請求中的細節被解耦放在不同的Interceptor中,不同Interceptor只負責自己的那一環節工作(對Request或者Response進行獲取/處理),使得攔截器模式完全貫穿整個網絡請求。
-
用戶可以添加自定義的Interceptor,okhttp把攔截器分爲應用攔截器和網絡攔截器
public class OkHttpClient implements Cloneable, Call.Factory {
final List<Interceptor> interceptors;
final List<Interceptor> networkInterceptors;
......
}
- 調用OkHttpClient.Builder的addInterceptor()可以添加應用攔截器,只會被調用一次,可以處理網絡請求回來的最終Response
- 調用addNetworkInterceptor()可以添加network攔截器,處理所有的網絡響應(一次請求如果發生了redirect ,那麼這個攔截器的邏輯可能會被調用兩次)
- Application interceptors與Network Interceptors
應用攔截器
不需要擔心中間過程的響應,如重定向和重試.
總是隻調用一次,即使HTTP響應是從緩存中獲取.
觀察應用程序的初衷. 不關心OkHttp注入的頭信息如: If-None-Match.
允許短路而不調用 Chain.proceed(),即中止調用.
允許重試,使 Chain.proceed()調用多次.
網絡連接器
能夠操作中間過程的響應,如重定向和重試.
當網絡短路而返回緩存響應時不被調用.
只觀察在網絡上傳輸的數據.
攜帶請求來訪問連接.
CopyFrom:
https://blog.csdn.net/fightingxia/article/details/70947701
https://www.jianshu.com/p/f5320b1e0287
Reference: