lundi 11 juillet 2016

Retrofit2 and RX how to output json error? (android)

Sorry for my English. This is my question: After the user goes through authorization, I send login and password to the server. When the operation succeeds, the server returns:

{
    "tok": "asd998asd8sa9das9d89sad99asd"
}

and when the operation fails, I get.

{
    "error": [
        "You are cant authorization"
    ]
}

How can I show the error to the user You are cant authorization.
My code:

@FormUrlEncoded
@POST(Links.URL_AUTHORIZATION)
Observable<ResponseBody> authorization(
        @Field("username") String username,
        @Field("password") String password);

I use:

 api.provideApi().authorization(login, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        //need here json from server. But how?
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        //Everething success
                    }
                });

Aucun commentaire:

Enregistrer un commentaire