简体   繁体   English

改进如何打印响应JSON

[英]Retrofit How To Print Out Response JSON

I'm using Retrofit and I want to get access to the JSON response that is returned from the server. 我正在使用Retrofit,我希望能够访问从服务器返回的JSON响应。 Could someone please advise me. 有人可以告诉我。 Thanks 谢谢

if you only want to see the respones for debugging purpose just turn on debugging in retrofit and look at the log. 如果您只想查看响应的调试目的,只需打开改造中的调试并查看日志。 It's something like: 它是这样的:

restAdapter.setDebuggingEnabled(true);

If you need access to the direct response in code all of the time, you should be doing that at the httpclient level as it's basically bypassing the purpose of retrofit 如果您需要始终在代码中访问直接响应,那么您应该在httpclient级别执行此操作,因为它基本上绕过了改造的目的

To print the response object first change the callback object type to the generic Object type via Callback<Object> cb . 要打印响应对象,首先通过Callback<Object> cb将回调对象类型更改为通用对象类型。 Then in your success callback you can just log the object to print the Json formatted version to the console. 然后在成功回调中,您可以只记录对象以将Json格式化版本打印到控制台。

@Override
public void success(Object o, Response response) {
    Log.i("Tag", "Login data " + o.toString());
}

To print the request object you can use whatever Json library you use (here I'm using Gson ) to serialize the request object to Json and log that to the console. 要打印请求对象,您可以使用您使用的任何Json库(这里我使用Gson )将请求对象序列化为Json并将其记录到控制台。

Log.i("Tag", "Request data " + new Gson().toJson(requestObject));

You need to get response in string format and then parse data using gson. 您需要以字符串格式获取响应,然后使用gson解析数据。 To get String response you can use CustomStringConverter by extending retrofit.converter.Converter or just get the inputstream from the response on success result. 要获得String响应,您可以通过扩展retrofit.converter.Converter来使用CustomStringConverter,或者只是从成功结果的响应中获取输入流。

public void success(Response r1, Response r2) {

    IntputStream in  = r1.getBody().in()
       //convert this inputstream to string
}

If you want to use full power of Retrofit and keep the raw input for yourself (eg to save it in persistent cache), you can extend the Converter , eg 如果你想使用Retrofit的全部功能并为自己保留原始输入(例如将其保存在持久缓存中),你可以扩展转换器 ,例如

RestAdapter restAdapter = new RestAdapter.Builder()
    .setConverter(new GsonConverter(gson) {
        @Override
        public Object fromBody(TypedInput body, Type type) throws ConversionException {
            try {
                byte[] buffer = new byte[body.length()];
                body.in().read(buffer);
                body.in().reset();
            }
            catch (IOException e) {
                throw new ConversionException(e);
            }
            return super.fromBody(body, type);
        }
    })
    ...build();
ResponseInterface ri = restAdapter.create(ResponseInterface.class);

You can also log the result like this 您也可以像这样记录结果

mRestAdapter = new RestAdapter.Builder()
            .setEndpoint(getBaseUrl())
            .setLogLevel(RestAdapter.LogLevel.FULL) //Log the result
            .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM