使用 Retrofit 2.0 POST 方法获取请求正文内容 [英] Getting Request body content using Retrofit 2.0 POST method

查看:55
本文介绍了使用 Retrofit 2.0 POST 方法获取请求正文内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在执行 enque 操作之前获取请求正文并使用 Retrofit 2.0 执行一些逻辑操作.但不幸的是,我无法从我的服务电话中获取帖子正文内容.目前,经过大量搜索后,我只找到了一种解决方案,例如 loggingrequest 我使用来自 此方法 使用 HttpLoggingInterceptorOkHttpClient.我正在使用以下代码在 Android Logcat 中记录请求正文:

I have a requirement to get a request body and to perform some logic operations with Retrofit 2.0 before doing enque operation. But unfortunately I am not able to get the post body content from my service call. At present after searching a lot I found only one solution like logging the request that I am posting using Retrofit 2.0 from this method by using HttpLoggingInterceptor with OkHttpClient. I am using the following code to log the request body in the Android Logcat:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
       logging.setLevel(Level.BODY);
    OkHttpClient httpClient = new OkHttpClient();
    httpClient.interceptors().add(logging);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(apiClass);

上述方法遇到的问题:

  1. 请求正文仅在默认的 Android Logcat 上可见,例如

02-04 01:35:59.235 5007-5035/com.example.retrofitdemo D/OkHttp:{"nameValuePairs":{"id":"1","name":"chandru","type":"user"}}

但上述方法仅返回 logcat 中的响应正文,我无法将其作为 StringJSONObject 获取.即使我能够使用 HttpLoggingInterceptor 获取响应正文,即使在应用程序投入生产之后,我的请求正文仍将始终显示在 Logcat 中并带有OkHttp"标签(所以主要是这个就像一种缓解logcat中的post数据一样).

But the above method returns only response body in the logcat, I am not able to get it as either String or JSONObject. Eventhough if I could able to get the response body using HttpLoggingInterceptor, my request body will be shown in the Logcat with the tag "OkHttp" all the time even after the application goes into the production (So primarily this leads like a kind of relieving the post data in the logcat).

我的要求:

我需要将请求正文作为 StringJSONObject 或任何方法获取,而不需要在 Logcat 中修改发布数据.

I need to get the request body as String or JSONObject or whatever method without reviling the post data in the Logcat.

我的尝试:

即使在 onResponse 之后,我也尝试从 Response 获取请求正文;响应,但尽管我无法完成它.请找到我为此目的使用的代码如下:

I tried to fetch the request body even after onResponse from Response<T> response, but though I couldn't able to get it done possible. Please find the code that I am using for this purpose as follows:

 Gson gson = new Gson();
 String responseString =  gson.toJson(response.raw().request().body());

注意:上面使用gson.toJson方法转换的请求体只返回元数据,而不是请求发布数据.

Note: The above converted request body using gson.toJson method returns only the meta data not the request post data.

请提供您宝贵的提示和解决方案来帮助我解决这个问题.我不知道该怎么做.在过去的两天里,我完全坚持了这一点.如果我的问题太长,请原谅.欢迎你提出任何建议.如果我的要求不清楚,请告诉我.提前致谢.

Kindly help me with this by providing your valuable tips and solutions. I have no idea how to do this. I am completely stuck up with this for the past two days. Please forgive if my question is too lengthy. Your comments are always welcome. Do let me know if my requirement is not clear. Thanks in advance.

推荐答案

我从这个链接得到了它Retrofit2:在 OkHttp 拦截器中修改请求体

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";
            }
        }

我正在使用的改造依赖是

Retrofit dependencies i am using are

 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

这篇关于使用 Retrofit 2.0 POST 方法获取请求正文内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆