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?
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.
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.
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.
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.
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 returnnull
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;
}
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With