Skip to content

seiginonakama/RetryRequestInterceptor-for-OkHttp

Repository files navigation

RetryRequestInterceptor for OkHttp2, OkHttp3


a RetryRequestInterceptor for okHttp2, OkHttp3, has those features:
  • will retry request until success or retry times over limit , or request live time over limit;
  • you can listen retry result by provide a RetryResultListener;
  • you can provide a encrypt storage to keep retry request safe;
  • invoke retry action by trigger, no loop thread, no waste cpu.

Import

repositories {
  jcenter()
}

for OkHttp2:

compile 'me.touko.okHttp2:RetryRequestInterceptorOkHttp2:1.0.0'

for OkHttp3:

compile 'me.touko.okHttp3:RetryRequestInterceptorOkHttp3:1.0.0'

Usage

Step 1 : init RetryRequestInterceptor before use

you can simple init RetryRequestInterceptor in Application.onCreate() method

  @Override
  public void onCreate() {
    super.onCreate();
    RetryRequestInterceptor.getInstance().init(this, new RetryConfig() {
      /**
       * min duration for retry request
       *
       * @return duration in unix times
       */
      @Override
      public long minRetryDuration() {
        return 1000L * 10;
      }

      /**
       * the life of retry request, if life < 0, request will live forever
       *
       * @return life in unix time
       */
      @Override
      public long life() {
        return -1; //the life of retry request, if life < 0, request will live forever
      }

      /**
       * max retry times of request, if maxRetryTimes < 0, will retry unLimit times
       *
       * @return max retry times
       */
      @Override
      public int maxRetryTimes() {
        return 10;
      }

      /**
       * judge whether to retry request when {@link #isSuccess(Request, Response)} return false
       *
       * @return whether to retry request
       */
      @Override
      public boolean isRetryRequest(Request request) {
        return request.url.toString().equals("http://api.exapmle.com/payresult/upload")
         && request.method.equals("POST"); //judge whether to retry request
      }
    }));
  }

Step 2 : add RetryRequestInterceptor to your OkHttpClient

    mOkHttpClient.interceptors().add(RetryRequestInterceptor.getInstance());

Step 3 : invoke retry action by random time point

you can simple invoke retry action in Activity.onPause() and Activity.onResume() method, or use my other library RandomEvent

@Override
public void onPause() {
  super.onPause();
  RetryRequestInterceptor.getInstance().retryTrigger();
}

@Override
public void onResume() {
  super.onResume();
  RetryRequestInterceptor.getInstance().retryTrigger();
}

(Optional)Step 4 : add RetryResultListener

    RetryRequestInterceptor.getInstance().addRetryResultListener(new RetryResultListener() {
      @Override
      public void onRetrySuccess(Request request, Response response) {
        
      }

      @Override
      public void onRetryError(Request request, IOException exception) {

      }

      @Override
      public void onRetryFailed(Request request, Response response) {

      }

      @Override
      public void onAbortRetry(Request request, long deadLine, int retryTimes) {

      }
    });

(Optional)Step 5 : Override more default RetryConfig method in Step 1 to make retry more customizable

RetryRequestInterceptor.getInstance().init(this, new RetryConfig() {
    
    ......
    
    /**
     * the storage to store retry requests, you can override this method to provide customize storage,
     * like encrypt storage {@link me.touko.okhttp.retryinterceptor.storage.EncryptFileStorage} etc..
     *
     * @return storage {@link Storage} {@link FileStorage}
     */
    protected Storage storage(Context context) {
      return new FileStorage(new File(context.getFilesDir(), "retryInterceptor").getAbsolutePath(), 0);
    }

    /**
     * the okHttpClient to send retry requests, you can override this method to provide your customize OkHttpClient
     *
     * @return okHttpClient {@link OkHttpClient}
     */
    protected OkHttpClient okHttpClient() {
      return new OkHttpClient();
    }

    /**
     * the default method to judge whether should retry request, you can override this method to judge by your logic
     *
     * @param request  the retry request
     * @param response the retry response
     * @return if return true, means the request retry succeed, else should continue retry
     */
    protected boolean isSuccess(Request request, Response response) {
      return response != null && response.isSuccessful();
    }
}

License

 Copyright (C) 2017 seiginonakama (https://github.com/seiginonakama).

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

About

a interceptor for OkHttp which can save failed request in storage and will retry request until success or retry times over limit , or request live time over limit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages