Retrofit 2.0 OnFailure - 原始响应 [英] Retrofit 2.0 OnFailure - Raw Response

查看:47
本文介绍了Retrofit 2.0 OnFailure - 原始响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 retrofit 来调用 Web 服务,但是改造失败了,来自Throwable"的消息给了我

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:预期为 BEGIN_OBJECT,但在第 1 行第 1 列路径 $

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

我假设这是因为 .Net Web 服务抛出错误而不返回 JSON.但是为了证明这一点,我需要能够在 onFailure 中看到原始响应.无论如何我可以做到这一点吗?

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?

这是我正在使用的代码

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


});

推荐答案

可以使用okhttp-logging-interceptor.

使用 Retrofit 2 进行日志记录 中也可以找到一个很好的例子.

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

这篇关于Retrofit 2.0 OnFailure - 原始响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆