All Projects → seiginonakama → RetryRequestInterceptor-for-OkHttp

seiginonakama / RetryRequestInterceptor-for-OkHttp

Licence: other
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

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to RetryRequestInterceptor-for-OkHttp

Chucker
🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+5064.29%)
Mutual labels:  okhttp, interceptor
Ok2curl
Convert OkHttp requests into curl logs.
Stars: ✭ 295 (+602.38%)
Mutual labels:  okhttp, interceptor
BothamNetworking
Networking Framework written in Swift.
Stars: ✭ 26 (-38.1%)
Mutual labels:  interceptor, retry-requests
Logginginterceptor
An OkHttp interceptor which has pretty logger for request and response. +Mock support
Stars: ✭ 1,149 (+2635.71%)
Mutual labels:  okhttp, interceptor
Okhttp Json Mock
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Stars: ✭ 231 (+450%)
Mutual labels:  okhttp, interceptor
OKHttpLogInterceptor
A Pretty OkHttp Logging Interceptor(一款简洁漂亮的OkHttp Logging拦截器)
Stars: ✭ 16 (-61.9%)
Mutual labels:  okhttp, interceptor
okhttp-awssigner
An OkHttp interceptor for signing requests with AWSv4 signatures
Stars: ✭ 14 (-66.67%)
Mutual labels:  okhttp, interceptor
superagent-intercept
Add functions that will be called during end() e.g. for handling error conditions without having the same code all over the place.
Stars: ✭ 23 (-45.24%)
Mutual labels:  interceptor
SunnyBeach
阳光沙滩APP
Stars: ✭ 60 (+42.86%)
Mutual labels:  okhttp
Android-Model-View-Presenter
No description or website provided.
Stars: ✭ 26 (-38.1%)
Mutual labels:  okhttp
rabbit
APP开发阶段:Mock API手机测试,支持局域网浏览器访问
Stars: ✭ 14 (-66.67%)
Mutual labels:  okhttp
FastHttpClient
封装OkHttp3,对外提供了POST请求、GET请求、上传文件、下载文件、https请求、cookie管理等功能
Stars: ✭ 60 (+42.86%)
Mutual labels:  okhttp
aws-s3-multipart-upload
Example AWS S3 Multipart upload with aws-sdk for Go - Retries for failing parts
Stars: ✭ 34 (-19.05%)
Mutual labels:  retry-requests
okir
A helper class that implements both an Espresso IdlingResource and an OkHttp Interceptor
Stars: ✭ 27 (-35.71%)
Mutual labels:  okhttp
kras
Efficient server proxying and mocking in Node.js. 💪
Stars: ✭ 18 (-57.14%)
Mutual labels:  interceptor
okhttp kit
dart版okhttp - https://github.com/square/okhttp
Stars: ✭ 16 (-61.9%)
Mutual labels:  okhttp
OkOne
基于okhttp库的网络性能优化框架
Stars: ✭ 88 (+109.52%)
Mutual labels:  okhttp
TMDB-App
Demo app using TMDB api
Stars: ✭ 13 (-69.05%)
Mutual labels:  okhttp
OkHttp3Utils
OkHttp3.0网络工具类
Stars: ✭ 34 (-19.05%)
Mutual labels:  okhttp
okhttp-eventsource
Server-sent events (SSE) client implementation for Java, based on OkHttp: http://javadoc.io/doc/com.launchdarkly/okhttp-eventsource
Stars: ✭ 70 (+66.67%)
Mutual labels:  okhttp

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].