我遇到了一些问题,不知道如何处理使用Retrofit和RxAndroid出现的响应错误。如果出现网络错误之类的情况会调用onError(),但我需要获得响应以检查是否存在身份验证错误。相反,我得到了一个空字符串的token,但无法找出原因。该怎么做才能解决这个问题?
这是我目前的RxAndroid调用。
Client.getInstance().getService()
.getToken(usernameET.getText().toString(), passwordET.getText().toString())
.subscribe(new Subscriber<SiteInfo>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(SiteInfo siteInfo) {
Log.d(TAG, "onNext "+ token.toString());
}
});
这是我的Retrofit服务。
@GET("my_url_here")
Observable<Token> getToken(
@Query("username") String username,
@Query("password") String password
);
这是我的当前restadapter。
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.build();
service = restAdapter.create(MyService.class);
这是我的 Token 类。
public class Token {
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@Override
public String toString() {
return token;
}
}
我该如何处理这个问题呢?它可以在RxAndroid部分完成吗?还是需要在我的RestClient中添加一些内容,或者完全需要其他东西?
谢谢。
07-01 06:38:04.562 1680-1793/uk.co.dyolo.thing D/Retrofit﹕ ---> HTTP GET my_website_here
07-01 06:38:04.562 1680-1793/uk.co.dyolo.thing D/Retrofit﹕ ---> END HTTP (no body)
07-01 06:38:04.610 1680-1793/uk.co.dyolo.thing D/Retrofit﹕ <--- HTTP 200 my_website_here (48ms)
07-01 06:38:04.610 1680-1793/uk.co.dyolo.thing D/Retrofit﹕ : HTTP/1.1 200 OK
{"error":"The username was not found in the database","stacktrace":null,"debuginfo":null,"reproductionlink":null}
。 - Radther200
状态码,但这并没有什么帮助。 - Radther