Android - How to get JSON request and response in Robospice / retrofit

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

My understanding:

The request is "githubRequest" and the response is "Contributor.List". 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 is: I want to check from the application whether the request was sent correctly to the service / response ("githubRequest" / "Contributor.List") JSON. So how can I request a JSON request and response. But the request / response is a POJO object. But if I want to print a JSON request and response, how can I do this? Anyone help me do this?

+2
json android web-services retrofit robospice
source share
2 answers

To print a response object, first change the type of the callback object to the generic type Object using Callback<Object> cb . Then in your callback, you can simply register the object to print a formatted version of Json on 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 any Json library you use (here I use Gson ) to serialize the request object to Json and put it in the console.

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

I think you can configure Retrofit to see the JSON request and response in the 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); } }); } 

}

0
source share

All Articles