Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓改型添加自定义标题_Android_Retrofit2 - Fatal编程技术网

Android 安卓改型添加自定义标题

Android 安卓改型添加自定义标题,android,retrofit2,Android,Retrofit2,我正在使用改型2,并希望使用OkHttp3 addInterceptor添加一些标题 但不起作用 这是我的密码 OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOExceptio

我正在使用改型2,并希望使用OkHttp3 addInterceptor添加一些标题 但不起作用

这是我的密码

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .removeHeader("Authorization")
                    .removeHeader("Content-type")
                    .removeHeader("User-Agent")
                    .addHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
                    .addHeader("Accept-Language", "en-US")
                    .addHeader("User-Agent", ApiConfig.userAgent)
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });
OkHttpClient client = httpClient.build();

    if (apiInterface == null) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiConfig.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        apiInterface = retrofit.create(ApiInterface.class);
    }
    return apiInterface;
请帮帮我。 亲切问候

如果有帮助,试试这个

RestAdapter.Builder builder = new RestAdapter.Builder()
    .setRequestInterceptor(new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json;versions=1");
            if (isUserLoggedIn()) {
                request.addHeader("Authorization", getToken());
            }                    
        }
    });

遵循此链接:不工作Yet将您的logcat置于其改装版1.9之上,但我使用的是v2