在这里插入代码片package com.bawei.weeklx.utils;
import android.util.Log;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* Created by Yezhicheng on 2019/2/23.
* 封装OKhttp和单列模式
*/
public class OkHttpUtils {
private static OkHttpUtils okHttpUtils =null;
public OkHttpUtils() {
}
public static OkHttpUtils getInstance(){
if (okHttpUtils==null){
//同步锁
synchronized(OkHttpUtils.class){
if (okHttpUtils==null){
okHttpUtils = new OkHttpUtils();
}
}
}
return okHttpUtils;
}
//创建OKhttp
public static void doGet(String url,Callback callback){
//创建日志拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("aaaa", "log: "+message);
}
});
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
封装单例模式和OkHttp网络请求&拦截器
最新推荐文章于 2021-06-03 00:28:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)