【问题标题】:How to add multiple headers with ok Http如何使用 okHttp 添加多个标头
【发布时间】:2016-03-01 16:41:40
【问题描述】:

我在我的 android 项目中使用 Retrofit 2 和 Okhttp。我想在 api 请求中添加多个标头。

这是我的拦截器代码:

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);
    }
}

这不能正常工作。在服务器端,我只得到最后添加的标题(在上面的例子中,我只得到 Securitykey missing "Userid" )

请帮忙。

【问题讨论】:

    标签: android okhttp retrofit2


    【解决方案1】:

    感谢支持 我找到了答案,这对我来说很好用

    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】:

    如果用户已经登录,您可以使用该类传递该类中的上下文。

      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;
                    }
                };
            }
    
    
        }
    

    【讨论】:

    • 这是完美的解决方案!!对于图像 ("Accept", "image/jpg") 和 ("Content-Type", "image/jpg") 效果理想!
    猜你喜欢
    • 2018-01-04
    • 1970-01-01
    • 2015-02-25
    • 2015-11-18
    • 1970-01-01
    • 2014-04-10
    • 2018-11-15
    • 1970-01-01
    • 2018-07-18
    相关资源
    最近更新 更多