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?
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));
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);
}
});
}
}
来源:https://stackoverflow.com/questions/24909759/android-how-to-get-json-format-request-and-response-in-robospice-retrofit