How to add multiple headers with ok Http

a 夏天 提交于 2020-06-27 07:59:10

问题


I am using Retrofit 2 and Okhttp for my android project. I want to add multiple headers in the api request.

This is my interceptor code :

public class NetworkInterceptors implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {

    Request request = chain.request().newBuilder()
            .addHeader("Userid", "10034")
            .addHeader("Securitykey", "Fb47Gi")
            .build();
    return chain.proceed(request);
    }
}

This is not working properly. In server side I am getting only the last added header (in the above example I am getting only Securitykey missing "Userid" )

Please Help.


回答1:


Thanks for support I found the answer, This is working fine for me

public class NetworkInterceptors implements Interceptor {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {

        Request request = chain.request();
        Request newRequest;

        newRequest = request.newBuilder()
                .addHeader("Userid", "10034")
                .addHeader("Securitykey", "Fb47Gi")
                .build();
        return chain.proceed(newRequest);
    }
}



回答2:


You can use this class pass the context in this class if user already logged in.

  public class ApiClient {
        public static final String BASE_URL = "";
        private static Retrofit retrofit = null;
        static Context mcontext;

        public static Retrofit getClient(Context context,String baseUrl)
        {

            mcontext = context;

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(220, TimeUnit.SECONDS)// Set connection timeout
                    .readTimeout(220, TimeUnit.SECONDS)// Read timeout
                    .writeTimeout(220, TimeUnit.SECONDS)// Write timeout
                    .addInterceptor( HeaderInterceptor() )

                  //  .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)// Add cache interceptor
                   // .cache(cache)// Add cache
                    .build();

            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .client(okHttpClient)
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
            return retrofit;
        }

        private static Interceptor HeaderInterceptor() {
            return new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    okhttp3.Request request = chain.request();
                    if(SharedPreference.getlogin(mcontext).equals("")){
                        request = request.newBuilder()
                                .addHeader("Accept", "application/json")
                                .addHeader("Authorization", "Bearer "+SharedPreference.gettoken(mcontext))
                                .build();
                    }
                    else {
                        request = request.newBuilder()
                                .addHeader("Accept", "application/json")
                                .build();
                    }


                    okhttp3.Response response = chain.proceed(request);
                    return response;
                }
            };
        }


    }


来源:https://stackoverflow.com/questions/35728484/how-to-add-multiple-headers-with-ok-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!