Conversion Error 2 + Rxjava

So, I already get a token from Json when the login is done without problems and gets a hash

But when the response from the server is an error, I can not get the Json message ({message: "Error: invalid letter"), because in onError we get only the Throwable argument, and not the model class, for example, in

how can i get json message from onError server ??

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

}

+7
json android retrofit rx-java error-handling
source share
2 answers

I found a solution, I have an application class that creates a modification, and then I just created a ResponseBody and converted using responseBodyConverter (,)

  @Override public void onError(Throwable e) { if (e instanceof HttpException) { ResponseBody body = ((HttpException) e).response().errorBody(); Converter<ResponseBody, Error> errorConverter = application.getRetrofit().responseBodyConverter(Error.class, new Annotation[0]); // Convert the error body into our Error type. try { Error error = errorConverter.convert(body); Log.i("","ERROR: " + error.message); mLoginView.errorText(error.message); } catch (IOException e1) { e1.printStackTrace(); } } static class Error{ String message; } 

the received JSon was {"error": 4041, "message": "Email not found"}, why the class is Error, I just created a variable message because I just need to show the message to the user

+7
source share

You are mixing a logical error (login error for some reason) with an application error. If your server sends a response with HTTP 200 and valid json in case of a failed login, your application will not raise onError at all.

So basically you need to check the (successful) response from the server and decide based on the provided json if the login was successful or not.

onError should be called, for example, if the server is unavailable or cannot execute the correct answer.

Edit: I don't know what your TokenResponse contains, but basically the login response should be the same, regardless of whether it was successful or not. Then you simply check the TokenResponse to see if the result of the error contains or not.

Mark this answer as an example: fooobar.com/questions/841047 / ...

+1
source share

All Articles