Retrofit 2.0 OnFailure - Commodity Response

I use retrofit to call a web service, and the modification fails, a message from "Throwable" gives me

java.lang.IllegalStateException: expected BEGIN_OBJECT, but there was STRING in row 1 column 1 path $

I assume this is due to the .Net web service throwing an error and not returning JSON. But to prove this, I need to see the original answer in onFailure . Anyway, can I do this?

this is the code i use

 public void userLoginRequestEvent(final AuthenticateUserEvent event) { Call call = sApi.login(event.getUsername(), event.getPassword(), OS_TYPE, DeviceInfoUtils.getDeviceName()); call.enqueue(new Callback<LoggedInUser>() { @Override public void onResponse(Response<LoggedInUser> response, Retrofit retrofit) { // response.isSuccess() is true if the response code is 2xx if (response.isSuccess()) { LoggedInUser user = response.body(); AppBus.getInstance() .post(new UserIsAuthenticatedEvent(user, event.getUsername(), event.getPassword())); } else { int statusCode = response.code(); // handle request errors yourself } } @Override public void onFailure(Throwable t) { // handle execution failures like no internet connectivity Log.d("ERROR", t.getMessage()); } }); 
+7
android retrofit
source share
2 answers

You can use the log hook that exists in okhttp-logging-interceptor .

A good example can be found in > Logging with Retrofit 2 .

+1
source share

The answer to your server is just a string, not an object. Use Interceptor to see the received answer.

Add an addict dependency

 compile 'com.squareup.okhttp3:logging-interceptor:3.4.0' 

and then add it to your custom OkHttp client.

 OKHttp client = .... HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client.interceptors().add(interceptor); Retrofit retrofit = new Retrofit.Builder() .baseUrl("url") .client(client) // add custom OkHttp client 

You can check out BASIC , HEADERS and BODY . In your case, you check BODY to see the body you are sending, and which server is sending as the response body.

+1
source share

All Articles