如何在改造2.0中添加公共参数? Intercepter只是可以添加标题参数

时间:2016-02-29 08:47:59

标签: android retrofit

public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    //add header params
    request = request.newBuilder().addHeader("header", "header").build();
    Log.d("retrofit request", request.url().toString());

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    String bodyString = response.body().string();
    Log.d("retrofit response",
            "request time " + (t2 - t1) / 1e6d + "ms\n" +
                    "request url " + response.request().url().toString() + "\n"
                    + "response body " + bodyString
    );

    return response.newBuilder()
            .body(ResponseBody.create(response.body().contentType(), bodyString))
            .build();
}

如何为所有请求添加get / post params?拦截可以只添加标题参数,我使用改造2.0 beta-1?有没有解决方案?

1 个答案:

答案 0 :(得分:0)

检查一下(Retrofit beta-4):

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        HttpUrl url = originalRequest.url().newBuilder().addQueryParameter("id", "value").build();
        return chain.proceed(originalRequest.newBuilder().url(url).build());
    }