【问题标题】:Android Retrofit 2.0.0-beta2 Post string body dynamic headersAndroid Retrofit 2.0.0-beta2 Post 字符串正文动态标头
【发布时间】:2024-01-02 16:03:01
【问题描述】:

改造 2.0.0-beta2

@Headers({
            "Authorization: {authorization}",
            "Content-Type: application/json"
    })
    @POST("/api/{id}/action/")
    Call<String> postfn(@Header("Authorization") String authorization, @Path("id") String id, @Body String body);

我正在使用 Gson 转换器 .addConverterFactory(GsonConverterFactory.create(gson))

我收到错误代码=400,消息=错误请求我应该使用自定义 转换器?

请帮忙

【问题讨论】:

    标签: android httpclient retrofit


    【解决方案1】:

    你不能把这两件事放在一起。

    有两种方法可以在改造 2.0 的请求上放置动态标头

    1:只放在方法签名中

    @Headers({
        "Content-Type: application/json"
    })
    @POST("/api/{id}/action/")
    Call<String> postfn(@Header("Authorization") String authorization, @Path("id") String id, @Body String body);
    

    2:使用请求拦截器添加固定的动态标头

    public class TraktvApiProvider implements Provider<TraktvApi> {
    
        public static final String BASE_URL = "https://api-v2launch.trakt.tv/";
    
        @Override
        public TraktvApi get() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build();
    
            retrofit.client().interceptors().add(new LoggingInterceptor());
    
            return retrofit.create(TraktvApi.class);
        }
    
        private class LoggingInterceptor implements Interceptor {
            @Override
            public Response intercept(Chain chain) throws IOException {
    
                Request request = chain.request();
    
                request = request.newBuilder()
                        .addHeader("trakt-api-version", "2")
                        .addHeader("trakt-api-key", "[YOUR-API-KEY]")
                        .build();
    
                Response response = chain.proceed(request);
    
                String bodyString = response.body().string();
    
                Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
                Log.d("Retrofit", String.format("%s - %s", request.method(), request.url()));
                Log.d("Retrofit", request.headers().toString());
                Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
                Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");
                Log.d("Retrofit", response.headers().toString());
                Log.d("Retrofit", "Body: " + bodyString);
                Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");
    
                return response.newBuilder()
                        .body(ResponseBody.create(response.body().contentType(), bodyString))
                        .build();
            }
        }
    }
    

    【讨论】:

    【解决方案2】:

    我回到稳定版 1.9 并且拦截工作正常。

    【讨论】: