导入依赖
implementation 'com.hjm:BottomTabBar:1.1.1'
implementation 'com.recker.flybanner:flybanner:1.3'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.2'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation'com.squareup.okhttp3:okhttp:3.7.0'
implementation'com.squareup.okio:okio:1.12.0'
implementation'com.android.support:design:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
代码如下
import android.util.Log;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.LogRecord;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import android.os.Handler;
public class OkHttp3 {
private OkHttpClient client;
private static volatile OkHttp3 instance;
private static Request request;
private static OkHttpClient okHttpClient;
private static Handler handler;
//l拦截器
private Interceptor getAppInterceptor(){
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.i("++++++", "拦截前: ");
Response response = chain.proceed(request);
Log.i("++++++", "拦截后: ");
return response;
}
};
return interceptor;
}
//构造方法
public OkHttp3() {
okHttpClient = new OkHttpClient()
.newBuilder()
.readTimeout(3000,TimeUnit.SECONDS)
.connectTimeout(3000,TimeUnit.SECONDS)
.addInterceptor(getAppInterceptor())//添加了拦截器
.build();
}
//单例OkHttp
public static OkHttp3 getInstance(){
if (instance==null){
synchronized (OkHttp3.class){
if (instance==null){
instance=new OkHttp3();
}
}
}
return instance;
}
public static void doGet(String url, final NetCallBack netCallBack){
handler = new Handler();
okHttpClient = new OkHttpClient();
request = new Request.Builder().url(url).method("GET", null).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.onSuccess(string);
}
});
}
});
}
public interface NetCallBack {
void onSuccess(String o);
}
}