Retrofit 2 synchronous call error handling for 4xx Errors

后端 未结 2 2077
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 15:00

I\'m using a android-priority-jobqueue and I use retrofit to make synchronous calls to my rest api but i\'m unsure how to handle errors like 401 Unauthorized errors which I sen

相关标签:
2条回答
  • 2021-02-13 15:42

    Putting checks for 401 on every response is not a very good approach. Instead one can apply this check at the base level i.e. while creating an object for Retrofit, through the interceptors. Have a look:

    public synchronized static Retrofit getClientWithRetry(final Context ctx) {
        if (clientWithRetry == null) {
            Interceptor responseCodeInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    Response response = chain.proceed(request);
                    if (response.code() == 401) {
                        Log.d(LOG_TAG, "Intercepted Req: " + response.toString());
                        Response r = retryWithFreshToken(request, chain);
                        return r;
                    }
                    return response;
                }
            };
    
            int cacheSize = 10 * 1024 * 1024; // 10 MB
            Cache cache = new Cache(ctx.getCacheDir(), cacheSize);
    
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(logging)
                    .addInterceptor(responseCodeInterceptor)
                    .cache(cache)
                    .build();
    
            Retrofit.Builder builder = new Retrofit.Builder()
                    .baseUrl(API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client);
            clientWithRetry = builder.build();
        }
        return clientWithRetry;
    }
    

    Here internally if a 401 is observed a new chained request can be made and token can be fetched. Post which the original request can be completed. Taken from this Retrofit retry tutorial.

    0 讨论(0)
  • 2021-02-13 15:48

    Check response code and show the appropriate message.

    Try this:

     PostService postService = ServiceGenerator.createService(PostService.class);
     final Call<Post> call = postService.addPost(post);
    
    Response<Post> newPostResponse = call.execute();
    
    // Here call newPostResponse.code() to get response code
    int statusCode = newPostResponse.code();
    if(statusCode == 200)
        Post newPost = newPostResponse.body();
    else if(statusCode == 401)
        // Do some thing... 
    
    0 讨论(0)
提交回复
热议问题