Okhttp中如何在攔截器中的RequestBody新增引數
阿新 • • 發佈:2019-02-11
參考:原文地址
1.可以遍歷formBody,迴圈新增 formBody
2.最好的辦法是重寫 FormBody,追加新增引數的方法。
OkHttpClient.Builder httpClient = new OkHttpClient.Builder() httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); //請求定製:新增請求頭 Request.Builder requestBuilder = original.newBuilder() .header("APIKEY", Constant.API_KEY); //請求體定製:統一新增token引數 if(original.body() instanceof FormBody){ FormBody.Builder newFormBody = new FormBody.Builder(); FormBody oidFormBody = (FormBody) original.body(); for (int i = 0;i<oidFormBody.size();i++){ newFormBody.addEncoded(oidFormBody.encodedName(i),oidFormBody.encodedValue(i)); } newFormBody.add("token",Constant.API_TOKEN); requestBuilder.method(original.method(),newFormBody.build()); } Request request = requestBuilder.build(); return chain.proceed(request); } });
特需字元
base64加密,那麼去掉字元
參考文章:參考地址