Retrofit2:修改OkHttp拦截器中的请求体

我在Android应用程序中使用了OkHttp客户端的Retrofit 2(2.0.0-beta3),到目前为止一切都很顺利。 但是目前我正面临着OkHttp拦截器的问题。 我与之通信的服务器是在请求体内取得访问令牌,所以当我需要添加auth令牌时,我拦截了添加auth令牌的请求或Authenticator的validation方法,为此我需要修改请求的主体。 但它看起来像我只能在标题中添加数据,而不是在正在进行的请求中。 我到目前为止写的代码如下:

client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (UserPreferences.ACCESS_TOKEN != null) { // need to add this access token in request body as encoded form field instead of header request = request.newBuilder() .header("access_token", UserPreferences.ACCESS_TOKEN)) .method(request.method(), request.body()) .build(); } Response response = chain.proceed(request); return response; } }); 

任何人都可以指出我正确的方向作为如何修改请求主体添加我的访问令牌(第一次或更新后令牌刷新)? 任何指向正确的方向将不胜感激。

Solutions Collecting From Web of "Retrofit2:修改OkHttp拦截器中的请求体"

我使用它来添加后现有的参数。

  OkHttpClient client = new OkHttpClient.Builder() .protocols(protocols) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder requestBuilder = request.newBuilder(); RequestBody formBody = new FormEncodingBuilder() .add("email", "Jurassic@Park.com") .add("tel", "90301171XX") .build(); String postBodyString = Utils.bodyToString(request.body()); postBodyString += ((postBodyString.length() > 0) ? "&" : "") + Utils.bodyToString(formBody); request = requestBuilder .post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString)) .build(); return chain.proceed(request); } }) .build(); public static String bodyToString(final RequestBody request){ try { final RequestBody copy = request; final Buffer buffer = new Buffer(); if(copy != null) copy.writeTo(buffer); else return ""; return buffer.readUtf8(); } catch (final IOException e) { return "did not work"; } } 

OkHttp3:

 RequestBody formBody = new FormBody.Builder() .add("email", "Jurassic@Park.com") .add("tel", "90301171XX") .build(); 

由于这不能写在@Fabian以前的答案的意见,我张贴这一个单独的答案。 这个答案处理“application / json”以及表单数据。

 import android.content.Context; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okio.Buffer; /** * Created by debanjan on 16/4/17. */ public class TokenInterceptor implements Interceptor { private Context context; //This is here because I needed it for some other cause //private static final String TOKEN_IDENTIFIER = "token_id"; public TokenInterceptor(Context context) { this.context = context; } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); RequestBody requestBody = request.body(); String token = "toku";//whatever or however you get it. String subtype = requestBody.contentType().subtype(); if(subtype.contains("json")){ requestBody = processApplicationJsonRequestBody(requestBody, token); } else if(subtype.contains("form")){ requestBody = processFormDataRequestBody(requestBody, token); } if(requestBody != null) { Request.Builder requestBuilder = request.newBuilder(); request = requestBuilder .post(requestBody) .build(); } return chain.proceed(request); } private String bodyToString(final RequestBody request){ try { final RequestBody copy = request; final Buffer buffer = new Buffer(); if(copy != null) copy.writeTo(buffer); else return ""; return buffer.readUtf8(); } catch (final IOException e) { return "did not work"; } } private RequestBody processApplicationJsonRequestBody(RequestBody requestBody,String token){ String customReq = bodyToString(requestBody); try { JSONObject obj = new JSONObject(customReq); obj.put("token", token); return RequestBody.create(requestBody.contentType(), obj.toString()); } catch (JSONException e) { e.printStackTrace(); } return null; } private RequestBody processFormDataRequestBody(RequestBody requestBody, String token){ RequestBody formBody = new FormBody.Builder() .add("token", token) .build(); String postBodyString = bodyToString(requestBody); postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody); return RequestBody.create(requestBody.contentType(), postBodyString); } }