Giter VIP home page Giter VIP logo

retryrequestinterceptor-for-okhttp's Introduction

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.

retryrequestinterceptor-for-okhttp's People

Contributors

seiginonakama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

retryrequestinterceptor-for-okhttp's Issues

I can't include the lib to my project

Hello! My issue is that I can't include the lib to my project. When I add 'me.touko.okHttp3:RetryRequestInterceptorOkHttp3:1.0.0' to my build.gradle file, Android Studio tells me that Failed to resolve: OkHttpRetryInterceptor:core:unspecified

Why do I get that problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.