简体   繁体   English

Retrofit / RxJava - 获取http响应代码

[英]Retrofit/RxJava - get http response code

Android, Retrofit, RxJava. Android,Retrofit,RxJava。 Please look at this example call: 请看这个示例调用:

 mcityService.signOut(signOutRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                    busyIndicator.dismiss();
                    finish();
                }, throwable ->
                {
                    Log.d(tag, throwable.toString());
                    busyIndicator.dismiss();
                    Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                    finish();
                });

Does anybody know how to get error code (as error number) from throwable? 有人知道如何从throwable获取错误代码(作为错误号)吗? I am able to get full stacktrace or message (as shown above). 我能够获得完整的堆栈跟踪或消息(如上所示)。 How to capture error code? 如何捕获错误代码?

Error code from throwable: throwable的错误代码: 在此输入图像描述

Just use casting?? 只是使用铸造?

mcityService.signOut(signOutRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                    busyIndicator.dismiss();
                    finish();
                }, throwable ->
                {
                    Log.d(tag, throwable.toString());
                    Log.d(code, ((HttpException)throwable).code());

                    busyIndicator.dismiss();
                    Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                    finish();
                });

Retrofit 2 + Rxjava handling error here is your answer 这里是Retrofit 2 + Rxjava处理错误的答案

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

see here for more . 请看这里了解更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM