okhttp的拦截器有很多,但无非就是两种类型,一种就是网络拦截器,一种是应用拦截器
具体步骤是一样,1.写一个okhttp的拦截器,常用压缩日志等拦截器,有写好的直接复制即可
2.创建拦截器的对象,
3.创建 OkHttpClient对象时,把拦截器设置进去即可
package com.sn.a1606bcat;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* 日志拦截器类,请求来了,先在这里进行处理,可以得到发请求到得到请求消耗多久的时间
* 作用:可以排查网络请求速度慢的根本原因
* 1.有可能是我们在请求网络时,客户端写了一堆业务逻辑
* 2.有可能是服务器端,写的有问题
* 3.有可能就是网速不给力
*/
class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
//拿到Request对象
Request request = chain.request();
long t1 = System.nanoTime();
System.out.println(" request = " + String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
//拿到Response对象
Response response = chain.proceed(request);
long t2 = System.nanoTime();
//得出请求网络,到得到结果,中间消耗了多长时间
System.out.println("response " + String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
在OkHttpUtils里面创建对象,添加
package com.sn.a1606bcat;
import android.os.Handler;
import android.os.Looper;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* date:2018/11/19
* author:dell
* function:
* 1.因为okhttp代码太多,太冗余,使用的时候不是太方便,两行代码搞定 -----doGet,doPost
* 2.都要创建OKhttp和咱们的handler对象,对象创建太多,导致内存过多的消耗 ----单例
* 3.异步请求okhttp,数据请求成功以后,数据在子线程,所以我们还要写handler,把数据放到主线程去,逻辑复杂 -----handler和接口
*
* 1. okhttp,handler,单例模式,接口
*
*/
public class OkhtttpUtilsTwo {
单例//
private final Handler mHandler;
private final OkHttpClient mOkHttpClient;
private static OkhtttpUtilsTwo sOkhtttpUtilsTwo;
//构造方法不私有
private OkhtttpUtilsTwo(){
//如果创建Handler的对象,是在一个普通的类里创建,那么一定要加上Looper.getMainLooper()这个参数
mHandler = new Handler(Looper.getMainLooper());
LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
mOkHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.build();
}
//单例暴露一个普通的方法,给对方,双重锁模式
public static OkhtttpUtilsTwo getInstance(){
if (sOkhtttpUtilsTwo ==null){
synchronized (OkhtttpUtilsTwo.class){
if (sOkhtttpUtilsTwo == null){
return sOkhtttpUtilsTwo =new OkhtttpUtilsTwo();
}
}
}
return sOkhtttpUtilsTwo;
}
//接口
public interface OkCallback {
void onFailure (Exception e);
void onResponse(String json);
}
okhttp与handler///
/封装doGEt的网络封装,参数定义两个,一个是URL网址 一个实现接口的对象
public void doGet(String url , final OkCallback ycfOkCallback){
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (ycfOkCallback !=null){
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
ycfOkCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response !=null && response.isSuccessful()){
final String json = response.body().string();
if (ycfOkCallback !=null){
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
ycfOkCallback.onResponse(json);
}
});
}
}
}
});
}
//封装doPost的逻辑代码
public void doPost(String url, Map<String,String> map, final OkCallback ycfOkCallback){
//创建FormBody对象,把表单添加到FormBody
FormBody.Builder builder = new FormBody.Builder();
//集合对象不为null的情况下
if (map != null){
for(String key: map.keySet()){
builder.add(key,map.get(key));
}
}
FormBody formBody = builder.build();
//创建Request对象
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (ycfOkCallback !=null){
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
ycfOkCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response !=null && response.isSuccessful()){
final String json = response.body().string();
if (ycfOkCallback !=null){
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
ycfOkCallback.onResponse(json);
}
});
}
}
}
});
}
}