OkHttp3的简单使用:
在Application中初始化OkHttp3
public class App extends Application { public static OkHttpClient okHttpClient; @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .build(); ImageLoader.getInstance().init(configuration); okHttpClient = new OkHttpClient(); okHttpClient = okHttpClient.newBuilder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .addInterceptor(new MyLogInterceptor()) .build(); } //拦截器,可以修改header,可以通过拦截器打印日志 public class MyLogInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .header("shenfen", "chinesse") .build(); HttpUrl url = request.url(); String httpUrl = url.url().toString(); Log.e("TAG", "============" + httpUrl); Response response = chain.proceed(request); int code = response.code(); Log.e("TAG", "============response.code() == " + code); return response; } } public static OkHttpClient send(){ return okHttpClient; } }在调用时,只需要 类名.方法名 就能得到需要的okhttp实例。