Android - How to get JSON format request and response in Robospice/retrofit
Asked Answered
C

2

2

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.

My Understanding:

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

Code Snippet:

    @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);
    }
}

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?

Casework answered 23/7, 2014 at 11:50 Comment(2)
Android Log size is limited, so if you do some big requests you may not be able to see some particular details you may be interested in. Personally I use Charles proxy to get the requests/responses, its quite easy to setup: jaanus.com/blog/2012/02/12/… . Maybe that will work for you as well.Ber
@Karolina Rusin: I just sent two parameters in the "githubRequest". is it possible to get the JSON request/response when using the Robospice/retrofit library?Casework
C
11

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());
}

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));
Cede answered 25/7, 2014 at 21:2 Comment(1)
Thanks for the response. I 'll check it.Casework
G
0

I think you can configure Retrofit in order to see JSON request and response in Log.

public class RetrofitSpiceService extends RetrofitGsonSpiceService {

private static final String BASE_URL = "http://your_url_here";

@Override
public void onCreate() {
    super.onCreate();
    addRetrofitInterface(SomeService.class);
}

@Override
protected String getServerUrl() {
    return BASE_URL;
}

@Override
protected Builder createRestAdapterBuilder() {

    Gson gson = new GsonBuilder()
            .create();

    return super.createRestAdapterBuilder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
                    // or .setLog(new AndroidLog("Retrofit"))
            .setLog(new RestAdapter.Log() {
                @Override
                public void log(String msg) {
                    String[] blacklist = {"Access-Control", "Cache-Control", "Connection", "Content-Type", "Keep-Alive", "Pragma", "Server", "Vary", "X-Powered-By",
                            "Content-Length", "Set-Cookie", "OkHttp-Selected-Protocol", "OkHttp-Sent-Millis", "OkHttp-Received-Millis"};
                    for (String bString : blacklist) {
                        if (msg.startsWith(bString)) {
                            return;
                        }
                    }
                    Log.d("Retrofit", msg);
                }
            });

}

}

Goose answered 2/10, 2015 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.