Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrofit & okhttpclient intercept 401 response

I'm trying to see whenever I get a response from my API with code 401. But when I do, i get an IOException

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);
    if (response.code() == 401) {
        mLoginToken.delete();
        Toast.makeText(mApplication.getApplicationContext(), R.string.session_error, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(mApplication.getApplicationContext(), LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        mApplication.startActivity(intent);
    }
    return response;
}

I will get the error java.io.IOException: unexpected end of stream on Connection{ proxy=DIRECT@ hostAddress= cipherSuite=none protocol=http/1.1} (recycle count=0)

At the line

        Response response = chain.proceed(request);

How should I get the responses with 401 (unauthorized code) in order to handle this?

like image 866
Boldijar Paul Avatar asked Feb 27 '16 20:02

Boldijar Paul


People also ask

What is Retrofit 2?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. We’ll not go into the details of Retrofit 1.x versions and jump onto Retrofit 2 directly which has a lot of new features and a changed internal API compared to the previous versions.

What does it mean to retrofit a device?

To outfit (a device, vehicle, building, or system) with newly developed or previously unavailable parts or equipment: retrofit cars with new safety features. 2. To install or fit (a device or system, for example) for use in or on an existing structure, especially an older dwelling.

What is @retrofit in okhttp?

Retrofit is the class through which your API interfaces are turned into callable objects. By default, Retrofit will give you sane defaults for your platform but it allows for customization. By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.

What is retrofitting and what are some examples?

Retrofitting is thus different from merely renovating, which may not involve any new technology at all. The factory has been retrofitted to meet the new safety regulations. We can retrofit your car with the new fuel system. Recent Examples on the Web The Sustenir Agriculture system is designed to retrofit into any building in Singapore and beyond.


1 Answers

I usually use inteceptors for requests only, and to handle errors set an error handler on the rest adapter builder, see example below:

Note: cause.getResponse() may return null

   yourRestAdapterBuilder.setErrorHandler(new ErrorHandler() {
                        @Override
                        public Throwable handleError(RetrofitError cause) {
                            switch (cause.getResponse().getStatus()) {
                                case 401:
                                    mLoginToken.delete();
                                    Toast.makeText(mApplication.getApplicationContext(), R.string.session_error, Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(mApplication.getApplicationContext(), LoginActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    mApplication.startActivity(intent);
                                default:
                                    // suppress or handle other errors
                                    break;
                            }
                        }
                    })
like image 109
Kevin Crain Avatar answered Nov 18 '22 03:11

Kevin Crain