/**
* Created by mumu on 2018/6/14.
*/
public class OkhtttpUtils {
private static OkhtttpUtils mOkhtttpUtils;
private OkHttpClient mOkHttpClien;
private final Handler mHandler;
private OkhtttpUtils() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Map<String, String> map = new HashMap<>();
map.put("source", "android");
PublicParamInterceptor publicParamInterceptor = new PublicParamInterceptor(map);
//创建一个主线程的handler
mHandler = new Handler(Looper.getMainLooper());
mOkHttpClien = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(publicParamInterceptor)
.build();
}
public static OkhtttpUtils getInstance() {
if (mOkhtttpUtils == null) {
synchronized (OkhtttpUtils.class) {
if (mOkhtttpUtils == null) {
return mOkhtttpUtils = new OkhtttpUtils();
}
}
}
return mOkhtttpUtils;
}
public void doGet(String url, final OkCallback okCallback) {
Request request = new Request.Builder()
.get()
.url(url)
.build();
final Call call = mOkHttpClien.newCall(request);
call.enqueue