Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2.0 OnFailure - Raw Response

I'm using retrofit to call a web service and retrofit is throwing a failure, the the message from the 'Throwable` is giving me

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I'm assuming that this is because the .Net web service is throwing an error and not returning JSON. But to prove this I need to be able to see the raw response in the onFailure. Is there anyway I can do this?

this is the code I'm using

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


});
like image 330
Flexicoder Avatar asked Oct 10 '15 11:10

Flexicoder


People also ask

Is retrofit restful?

Overview. Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square.

What is retrofit2?

What is Retrofit. Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.


1 Answers

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

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

like image 197
keno Avatar answered Oct 22 '22 06:10

keno