OkHttpClient throws exception after upgrading to OkHttp3

后端 未结 2 1978
一生所求
一生所求 2020-12-25 11:21

I\'m using following lines of code to add a default header to all of my requests sent using Retrofit2:

private static OkHttpClient defaultHttpClient = new Ok         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-25 11:58

    You have to use builder if you want to create OkHttp(3)Client object.

    Try change this:

    private static OkHttpClient defaultHttpClient = new OkHttpClient();
    

    To something like this:

      OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
           .addInterceptor(
               new Interceptor() {
                 @Override
                 public Response intercept(Interceptor.Chain chain) throws IOException {
                       Request request = chain.request().newBuilder()
                       .addHeader("Accept", "Application/JSON").build();
                   return chain.proceed(request);
                  }
               }).build();
    

提交回复
热议问题