改装POST。向每个发布请求添加JSON参数

时间:2019-04-24 10:58:03

标签: java android retrofit retrofit2 okhttp3

我有四个静态参数,我只需要为每个后期改造请求设置一次,因为我不需要每次都将它们与每个请求一起放入。这是我的代码:

  public interface Login {
        @POST("login")
        @Headers("Content-Type: application/json")
        Call<String> login(@Body JsonObject json);
    }

然后我将其命名并通过电子邮件和密码作为正文参数,如下所示:

  private void sendLoginRequest() {
    API.login().login(CommandLogin.getInputParamsAsJSON(mStrEmail, Utils.md5(mStrPassword))).enqueue(this);
}

然后在拦截主体中尝试传递此静态参数,但是当我检查 log 时,似乎仅传递了这两个静态参数,而没有电子邮件和密码。在我的情况下,我想放入正文 电子邮件密码操作系统版本

public class API {

private static <T> T builder(Class<T> endpoint) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain){
                            try {
                                Request original = chain.request();

                                JSONObject params = new JSONObject();
                                params.put("version"        , "6");
                                params.put("os"             , "2");

                                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                                RequestBody body = RequestBody.create(JSON, params.toString());

                                Request.Builder requestBuilder = original.newBuilder()
                                        .post(body);

                                Request request = requestBuilder.build();

                                return chain.proceed(request);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //TODO
                            return null;
                        }
                    })
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build();

    return new Retrofit.Builder()
            .baseUrl(Utils.BASE_COMPONENT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build()
            .create(endpoint);
}



public static Login login() {
    return builder(Login.class);
}

}

1 个答案:

答案 0 :(得分:1)

这是因为,您基本上是在用新的参数覆盖原始的Login Post Request Body。相反,您应该做的是,从原始请求中获取帖子正文,并为其添加新的参数。

类似以下内容(代码未经测试)

try {
    // Create new buffer
    final Buffer buffer = new Buffer();

    // Copy original request's json body to buffer
    chain.request().body().writeTo(buffer);

    // Convert buffer to JSONObject
    JSONObject params = new JSONObject(buffer.readUtf8());

    // Append new params
    params.put("version", "6");
    params.put("os", "2");

    // Now the params json object will have all 4 values: email,password,os,version

    // TODO: Your remaining code

} catch (Exception ex) {
    Log.d("Login","Error creating post body", ex);
}