From b49ea3458575ae456ba75119519403ec43df74b8 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Wed, 11 Apr 2018 16:25:15 +0800 Subject: [PATCH] fix(http): timeout on http request --- autojs/src/main/assets/modules/__http__.js | 9 ++- .../autojs/core/http/MutableOkHttp.java | 75 +++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 autojs/src/main/java/com/stardust/autojs/core/http/MutableOkHttp.java diff --git a/autojs/src/main/assets/modules/__http__.js b/autojs/src/main/assets/modules/__http__.js index 54230538..60806f6f 100644 --- a/autojs/src/main/assets/modules/__http__.js +++ b/autojs/src/main/assets/modules/__http__.js @@ -1,6 +1,10 @@ module.exports = function(runtime, scope){ importPackage(Packages["okhttp3"]); + importClass(com.stardust.autojs.core.http.MutableOkHttp); var http = {}; + + http.__okhttp__ = new MutableOkHttp(); + http.get = function(url, options, callback){ options = options || {}; options.method = "GET"; @@ -8,10 +12,7 @@ module.exports = function(runtime, scope){ } http.client = function(){ - if(!http._client_){ - http._client_ = new OkHttpClient(); - } - return http._client_; + return http.__okhttp__.client(); } http.post = function(url, data, options, callback){ diff --git a/autojs/src/main/java/com/stardust/autojs/core/http/MutableOkHttp.java b/autojs/src/main/java/com/stardust/autojs/core/http/MutableOkHttp.java new file mode 100644 index 00000000..668ec32b --- /dev/null +++ b/autojs/src/main/java/com/stardust/autojs/core/http/MutableOkHttp.java @@ -0,0 +1,75 @@ +package com.stardust.autojs.core.http; + +import java.io.IOException; +import java.util.Collections; +import java.util.concurrent.TimeUnit; + +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +/** + * Created by Stardust on 2018/4/11. + */ + +public class MutableOkHttp extends OkHttpClient { + + private OkHttpClient mOkHttpClient; + private int mMaxRetries = 3; + private long mTimeout = 30 * 1000; + private Interceptor mRetryInterceptor = chain -> { + Request request = chain.request(); + Response response = chain.proceed(request); + int tryCount = 0; + while (!response.isSuccessful() && tryCount < getMaxRetries()) { + tryCount++; + response = chain.proceed(request); + } + return response; + }; + + public MutableOkHttp() { + mOkHttpClient = newClient(new OkHttpClient.Builder()); + } + + public OkHttpClient client() { + return mOkHttpClient; + } + + protected OkHttpClient newClient(Builder builder) { + builder.readTimeout(getTimeout(), TimeUnit.MILLISECONDS) + .writeTimeout(getTimeout(), TimeUnit.MILLISECONDS) + .connectTimeout(getTimeout(), TimeUnit.MILLISECONDS); + for (Interceptor interceptor : getInterceptors()) { + builder.addInterceptor(interceptor); + } + return builder.build(); + } + + public Iterable getInterceptors() { + return Collections.singletonList(mRetryInterceptor); + } + + public int getMaxRetries() { + return mMaxRetries; + } + + public void setMaxRetries(int maxRetries) { + mMaxRetries = maxRetries; + } + + public long getTimeout() { + return mTimeout; + } + + + public void setTimeout(long timeout) { + mTimeout = timeout; + muteClient(); + } + + protected synchronized void muteClient() { + mOkHttpClient = newClient(mOkHttpClient.newBuilder()); + } +}