37. OKhttp自定义配置
37.1 Builder构建者
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
提供更多操作
37.2 拦截器
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new xxxx).build();
OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new xxxx).build();
一个前面执行,一个后面执行
测试
package com.dingjiaxiong.myokhttp;
import android.util.Log;
import androidx.annotation.NonNull;
import org.junit.Test;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class InterceptorUnitTest {
@Test
public void interceptorTest() {
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
//前置处理
Request request = chain.request().newBuilder().addHeader("os", "Android")
.addHeader("version", "1.0").build();
Response response = chain.proceed(request);
//后置处理
return response;
}
}).build();
//只要通过这个 okHttpClient 发起的请求,都会经过拦截器。
Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
addNetworkInterceptor和addInterceptor的执行顺序
说明addInterceptor拦截器优先执行。与代码书写时前后顺序无关。
可以添加任意多个拦截器。
37.3 缓存与Cookie
OKhttp按照http协议的规则实现了缓存的处理
当发起第一次请求之后,如果后续还需要进行同样的请求,此时如果符合缓存规则,则可以减少与服务器的网络通信,直接从本地文件缓存中读取响应返回给请求者。
【默认情况下,OKhttp的缓存是关闭状态,需要开启】
配置这个后,自动开启缓存
cookie是某些网站为了辨别用户身份,进行会话跟踪(比如确定登录状态)而存储在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息。
玩安卓:https://www.wanandroid.com/
开放API
cookie使用演示
package com.dingjiaxiong.myokhttp;
import androidx.annotation.NonNull;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class CookieUnitTest {
//用全局变量接收
Map<String , List<Cookie>> cookies = new HashMap<>();
@Test
public void cookieTest() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(@NonNull HttpUrl httpUrl, @NonNull List<Cookie> list) {
cookies.put(httpUrl.host(),list);
}
@NonNull
@Override
public List<Cookie> loadForRequest(@NonNull HttpUrl httpUrl) {
if (httpUrl.host().equals("www.wanandroid.com")) {
List<Cookie> cookies = CookieUnitTest.this.cookies.get(httpUrl.host());
return cookies == null ? new ArrayList<>() : cookies;
}
return new ArrayList<>();
}
})
.build();
FormBody formBody = new FormBody.Builder()
.add("username", "dingjiaxiong")
.add("password", "200039").build();
Request request = new Request.Builder().url("https://www.wanandroid.com/user/login")
.post(formBody)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
//请求收藏文章列表接口
request = new Request.Builder().url("https://www.wanandroid.com/lg/collect/list/0/json")
.build();
call = okHttpClient.newCall(request);
try {
Response response = call.execute();
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}