Android 4.2.2上的RetrofitError响应为空

时间:2015-02-03 07:52:29

标签: android retrofit

我正在Android项目中使用Retrofit库。我有一个休息服务器,并尝试向它的登录方法发出请求。如果登录失败,服务器会在json中返回401错误和详细消息。我的问题是在我的Android版Nexus 5上工作正常,我可以从RetrofitError对象获取响应主体。但在其他设备上,例如Sony Xperia M和Android 4.2.2在同一请求中,我得到No authentication challenges found异常,而RetrofitError.response对象为null。有什么想法吗?

更新

这是我的代码。我也使用Robospice库。

所以这是一个界面:

@POST("/auth")
User logIn(@Body UserSignUp userSignUp);

这是我的Robospice请求类

public class UserLoginRequest extends RetrofitSpiceRequest<User, Server> {

private UserSignUp userSignUp;

public UserLoginRequest(UserSignUp userSignUp) {
    super(User.class, Server.class);
    this.userSignUp = userSignUp;
}

@Override
public User loadDataFromNetwork() throws Exception {
    return getService().logIn(userSignUp);
}
}

这是我的请求听众:

public final class UserLoginListener implements RequestListener<User> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        if (spiceException.getCause() != null && ((RetrofitError) spiceException.getCause()).getBody() != null) {
            Toast.makeText(LoginActivity.this, ((ResponseDto) (((RetrofitError) spiceException.getCause()).getBody())).error, Toast.LENGTH_LONG).show(); //On Nexus 5 with Android 5 this line works fine
        } else {
            Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show(); //On other devices I'm getting to this
        }
    }

    @Override
    public void onRequestSuccess(User user) {
        //Some code here
    }
}

UserResponseDto类的子类。 ResponseDto类只有一个名为error的公共字符串字段。

1 个答案:

答案 0 :(得分:0)

似乎此异常与版本相关 - 它是HttpURLConnection抛出的IOException。尝试更改服务器的行为或在改进时处理IOException。

  

未发现身份验证质询

     

此错误发生在服务器发送401(未授权)但未提供WWW-Authenticate标头,这是客户端下一步操作的提示。

看看here

相关问题