Set Headers uniformly under Retrofit2 + OkHttp3

In recent days, I tried to use Retrofit and OkHttp to build the network layer. After configuring the dependency link from the official website, I was surprised to find that OkHttp3 and Retrofit2 are not compatible with the previous version, not only the package name is different (OkHttp3.*, the previous version is com .squareup.okhttp.*) and many methods have been deleted. At present, only Retrofit2 has a little information on the Internet, and OkHttp3 can only refer to official documents.

One problem encountered when building the network layer is to manually configure the Headers of the Http request, write cache cookies, custom User-Agent and other parameters, but for the network layer with dozens of interfaces, I don’t want to use annotation configuration Headers, the methods of many articles on the Internet are really not very suitable for these two versions. Some methods have been deleted, and some methods will report exceptions:(

After reading the official API documentation, the method is as follows:

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
/**
*shu_quan
*/
public class RetrofitAPIManager {

    public static final String SERVER_URL = "url";

    public static ClientAPI provideClientApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(SERVER_URL)
                .client(genericClient())
                .build();
        return retrofit.create(ClientAPI.class);
    }

//可添加Cookie
    public static OkHttpClient genericClient() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                                .addHeader("Accept-Encoding", "gzip, deflate")
                                .addHeader("Connection", "keep-alive")
                                .addHeader("Accept", "*/*")
                                .addHeader("Cookie", "add cookies here")
                                .build();
                        return chain.proceed(request);
                    }

                })
                .build();

        return httpClient;
    }
}
That is to use Interceptor to intercept and reset the request header, the test is available

/**Official doc search record**/

  • The List<Interceptor> interceptors() method of OkHttpClient under OkHttp3 returns an uneditable list. If you edit it, it will report UnSupportedOperationException
  • The typical use scenario of Interceptor is to edit the headers of request and response

Guess you like

Origin blog.csdn.net/shu_quan/article/details/79877258