dimanche 10 juillet 2016

Retrofit2 and RX how output json error?(android)

Sorry for my english. Now i learn for me new teknology and for me not clearly one moment. For example, i have authorization and i send login and password to server when i have succes server say me:

{
    "tok": "asd998asd8sa9das9d89sad99asd"
}

when i have error server say me:

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

How i can output error for 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