9
votes

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


});
2
Try logging the request/response using approaches at stackoverflow.com/questions/32514410/logging-with-retrofit-2keno
Please provide full stacktrace listing, as well as listing of api interface. You can print full stacktrace by adding t.printStackTrace() in your onFailure(Throwable t) method. Didn't notice how old this question is. Is it still not solved? :)Клаус Шварц
@КлаусШварц it was solved by the first comment, but there was no answer posted that I could acceptFlexicoder
@keno - if you wanted to post that as an answer I can accept it?Flexicoder
@Flexicoder I became a bit curious about your question itself and did a small research of retrofit2 usage without reactive streams (I prefer rx implementation and more experienced with it). It seems for me that current API had changed and is different from what it had been back in 2015. I think the best option here is if keno will post an answer and you will mark it as accepted, or you will answer you question yourself. The only reason I commented this old question was that it is still unanswered and has rather big amount of votes. Thank you.Клаус Шварц

2 Answers

2
votes

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.

2
votes

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

Add incerceptor 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 for BASIC, HEADERS and BODY. In your case you check for BODY to see body that you send and what server is sending as response body.