目录
前言
一、创建拦截器
二、缓存
三、Cookie
前言用OkhttpClient自定义配置属性,需要创建OkHttpClient的内部类Builder()的是实例对象。然后使用Builder()的方法进行自定义配置。本文将为OkHttp设置拦截器,缓存以及Cookie。本文将在Android Studio的Test文件中执行。(也可以结合上一篇文章进行拦截器的设置)
一、创建拦截器当进行多次请求时,需要多次创建请求头,此时可以采用拦截器进行统一设置。直接在拦截器中设置请求头等。
1.OkHttpClient okHttpClient = new OkHttpClient.Builder.addInterceptor(new xxx).build();
2.OkHttpClient okHttpClient = new OkHttpClient.Builder.addNetWorkInterceptor(new xxx).build();
addInterceptor方法可以多次添加;
addInterceptor总是优先于addNetWorkInterceptor执行;
public class InterceptorUnitTest {
@Test
public void CookieTest() {
OkhttpClient okhttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("version","1.0")
.addHeader("os","android").build();
Response response = chain.proceed(request);
return response;
}
}).build();
new Thread(){
@Override
public void run() {
FormBody formBody = new FormBody.Builder().add("a","1").add("b","2").build();
//请求体,将参数加在请求体当中
Request request = new Request.Builder().url("https://www.httpbin.org/post").post(formBody).build();
Call call = okhttpClient.newCall(request);
try {
Response response = call.execute();
Log.i("yyy", "PostSync: "+response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
二、缓存
Okhttp按照http协议规则实现了缓存的处理,当用户发起第一次请求之后,如果后序海需要进行同样的请求,此时如果符合缓存规则,则可以减少于服务器的网络通信,直接从本地文件缓存中读取响应返回给请求者,。默认情况下OkHttp的缓存是关闭状态,需要用户开启。
OkhttpClient.Builder().cache(new Cache(new File(pathname),maxSize)); 设置缓存,pathname:缓存文件的地址;maxSize:缓存最大容量字节
直接在上面的代码中添加cache(xxx);
"D:\\File\\fos.txt":为自定义文件名的路径,可以自行设置
OkHttpClient okhttpClient = new OkHttpClient.Builder().cache(new Cache(new File("D:\File\fos.txt"),1024*1024)).addInterceptor(new Interceptor() {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("version","1.0")
.addHeader("os","android").build();
Response response = chain.proceed(request);
return response;
}
}).build();
三、Cookie
Cookie是网站为了辨别用户身份,进行会话跟踪(比如登录状态)而存储在用户本地终端上的数据(通常经过加密),由用户
public class CookieUniTst {
Map> cookies = new HashMap<>();
@Test
public void CookieTest() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(@NonNull HttpUrl httpUrl, @NonNull List list) {
cookies.put(httpUrl.host(),list);
}
@NonNull
@Override
public List loadForRequest(@NonNull HttpUrl httpUrl) {
List cookies = CookieUniTst.this.cookies.get(httpUrl.host());
return cookies==null?new ArrayList<>():cookies;
}
})
.build();
FormBody formBody = new FormBody.Builder().add("username","自己的用户名")
.add("password","自己的密码").build();
Request request = new Request.Builder().post(formBody).url("https://www.wanandroid.com/user/login").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();
}
}
}
客户端计算机暂时或永久保存的信息。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)