2015-11-06 48 views
3

到requestbody我有這:改造2追加後在截距

OkHttpClient client = new OkHttpClient(); 
client.interceptors().add(new Interceptor() { 
    @Override 
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     HttpUrl url = request.httpUrl().newBuilder() 
        .addQueryParameter("platform", "android") 
        .addQueryParameter("app_version", com.package.BuildConfig.VERSION_NAME) 
        .build(); 
     Request newRequest = chain.request().newBuilder().url(url).build(); 
     return chain.proceed(newRequest); 
    } 
}); 

但還要額外交鍵值附加到包含用戶鑰請求主體。這看起來像

RequestBody newBody = RequestBody.create(request.body().contentType(),request.body().content+ request.addPost("sUserKey","3254345kdskf"); 
... 
... 
Request newRequest = chain.request() 
.newBuilder() 
.url(url) 
.post(newBody) 
.build(); 
+0

有什麼問題,怎麼做呢? – Blackbelt

+0

在okhttp攔截過程中,您將如何添加額外的POST? –

+0

你想要對不同的端點發布post請求嗎? – Blackbelt

回答

2

你可以做到這一點,而無需創建額外的類。

client.interceptors().add(new Interceptor() { 
    @Override 
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     String parameter = "&" + name + "=" + value; 
     Request newRequest = interceptRequest(request, parameter) 
     return chain.proceed(newRequest); 
    } 
}); 

這是創建新請求的簡單方法。

public static Request interceptRequest(@NotNull Request request, @NotNull String parameter) 
      throws IOException { 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     Sink sink = Okio.sink(baos); 
     BufferedSink bufferedSink = Okio.buffer(sink); 

     /** 
     * Write old params 
     * */ 
     request.body().writeTo(bufferedSink); 

     /** 
     * write to buffer additional params 
     * */ 
     bufferedSink.writeString(parameter, Charset.defaultCharset()); 

     RequestBody newRequestBody = RequestBody.create(
       request.body().contentType(), 
       bufferedSink.buffer().readUtf8() 
     ); 

     return request.newBuilder().post(newRequestBody).build(); 
    } 

您也可以從Gist

5

追加到RequestBody不是直截了當的。這裏是一個自定義RequestBody的草圖,它將添加一個post參數。幾個警告 - 你會想添加一些錯誤檢查,如確保現有的身體不爲空。給定的代碼還假定所有來這個攔截器的調用都是POST。如果不是這種情況,則在應用新正文之前,您需要檢查傳入的請求類型。另外,由於這只是將新參數複製到主體中,所以如果需要,您需要確保名稱和主體已經被url編碼。

class AddPostParamRequestBody extends RequestBody { 

    final RequestBody body; 
    final String parameter; 

    AddPostParamRequestBody(RequestBody body, String name, String value) { 
     this.body = body; 
     this.parameter = "&" + name + "=" + value; 
    } 

    @Override 
    public long contentLength() throws IOException { 
     return body.contentLength() + parameter.length(); 
    } 

    @Override 
    public MediaType contentType() { 
     return body.contentType(); 
    } 

    @Override 
    public void writeTo(BufferedSink bufferedSink) throws IOException { 
     body.writeTo(bufferedSink); 
     bufferedSink.writeString(parameter, Charset.forName("UTF-8")); 
    } 

} 

然後你就可以在你的攔截器使用 -

client.interceptors().add(new Interceptor() { 
    @Override 
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     HttpUrl url = request.httpUrl().newBuilder().addQueryParameter("added", "param").build(); 
     AddPostParamRequestBody newBody = new AddPostParamRequestBody(request.body(), "sUserKey","3254345kdskf"); 
     Request newRequest = request.newBuilder().post(newBody).url(url).build(); 
     return chain.proceed(newRequest); 
    } 
}); 

你的另一種選擇是將包括在您的改裝定義一個額外的Field註釋,並通過它在每次通話,但我相信你試圖避免這種情況。