Android的 - 如何获得Robospice /改造JSON格式的请求和响应 [英] Android - How to get JSON format request and response in Robospice/retrofit

查看:505
本文介绍了Android的 - 如何获得Robospice /改造JSON格式的请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Robospice /改装库。我从GitHub的一些样品。的https://github.com/octo-online/RoboSpice-samples/tree/release/robospice-sample-retrofit.

I am new to the Robospice/retrofit libraries. I got some samples from the github. https://github.com/octo-online/RoboSpice-samples/tree/release/robospice-sample-retrofit.

我的理解:

的请求是githubRequest和响应是Contributor.List。 web服务是由getSpiceManager()调用。执行。

The request is "githubRequest" and the response is the "Contributor.List". The webservice is called by getSpiceManager().execute.

code段:

    @Override
        protected void onStart() {
            super.onStart();
            getSpiceManager().execute(githubRequest, "github", DurationInMillis.ONE_MINUTE, new ListContributorRequestListener());
    } 

public final class ListContributorRequestListener implements RequestListener<Contributor.List> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        Toast.makeText(SampleSpiceActivity.this, "failure", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(final Contributor.List result) {
        Toast.makeText(SampleSpiceActivity.this, "success", Toast.LENGTH_SHORT).show();
        updateContributors(result);
    }
}

我的问题:我想从应用程序进行检查,是否请求/响应(githubRequest/Contributor.List)正确JSONs发送到服务。所以,我怎么能SYSOUT JSON的请求和响应。但请求/响应是POJO对象。但是如果我想打印的JSON请求和响应,我该怎么办呢?
任何人帮助我​​做到这一点?

My Question: I want to check from the app, whether the request/response("githubRequest"/"Contributor.List") correct JSONs are sent to the service. So How can I sysout the JSON request and response. But the request/response are POJO object. But If I want to print the JSON request and response, How can I do that? Anybody help me to do this?

推荐答案

要打印响应对象第一个回调对象类型更改为普通的对象通过<$ C型$ C>回调&LT;对象&gt; CB 。然后在你的成功回调你可以登录到打印JSON格式的文本控制台的对象。

To print the response object first change the callback object type to the generic Object type via Callback<Object> cb. Then in your success callback you can just log the object to print the Json formatted version to the console.

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

要打印,您可以使用您使用任何的JSON库(在这里我使用 GSON <请求对象/ A>)序列化请求对象为JSON并记录到控制台上。

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.

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

这篇关于Android的 - 如何获得Robospice /改造JSON格式的请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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