So i already receive a token from the Json when the login is made without problems, and receive the hash
But when the response from the server is a error, i cannot get the Json message ({message:"Error : wrong email} " because in the onError we only get as argument a Throwable and not a model class like in on
how can i get a json message from server onError??
final Observable<TokenResponse> observable = Service.login(userCredentials);
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<TokenResponse>() {
String error="";
@Override
public void onCompleted() {
mLoginView.whenLoginSucess();
}
@Override
public void onError(Throwable e) {
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
Response response = exception.response();
Converter<ResponseBody, ErrorFromServer> converter = new GsonConverterFactory()
.responseBodyConverter(ErrorFromServer.class, Annotation[0]);
ErrorFromServer error = converter.convert(response.errorBody());
}
mLoginView.errorText(error);
e.printStackTrace();
}
@Override
public void onNext(TokenResponse tokenResponse) {
SharedPreferences.Editor editor = sharedP.edit();
editor.putString("hash", tokenResponse.getToken());
editor.commit();
//getString return key value "hash" if there is no value, returns null
hash = sharedP.getString("hash",null);
}
});
TokenResponse code
public class TokenResponse {
@SerializedName("hash")
@Expose
private String token;
@SerializedName("message")
@Expose
private String message;
public String getMessage() {return message;}
public String getToken() {
return token;
}
}