I am having trouble working out how to handle a response error with retrofit and RxAndroid. onError() gets called if there is a network error or the like but I need to be able to get the response to check if there was an authentication error. Instead what I get is a token with a null String and I can't find out why. What is the best way to go about this?
This is my RxAndroid call at the moment.
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());
}
});
This is my retrofit service
@GET("my_url_here")
Observable<Token> getToken(
@Query("username") String username,
@Query("password") String password
);
This is my Current restadapter
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.build();
service = restAdapter.create(MyService.class);
And this is my Token Class.
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;
}
}
How should I go about handlng this. Can it be done in the RxAndroid part or do I need to add something to my RestClient or maybe something else entirely?
Thanks.
Edit
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
authentication error
? Retrofit's observable emits error if there was status other than200
. – eleven{"error":"The username was not found in the database","stacktrace":null,"debuginfo":null,"reproductionlink":null}
is the response I get. – Radtherhttp
. – eleven200
code then, which isn't very helpful. – Radther