From 0354c1581e397023f54c2d906e0ee372de2b9604 Mon Sep 17 00:00:00 2001 From: Amiir Date: Wed, 4 Sep 2019 14:26:24 +0430 Subject: [PATCH] add write time out --- .../com/github/restclient/MainActivity.java | 6 +++- build.gradle.kts | 6 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/com/github/library/BaseClient.java | 11 ++++--- .../java/com/github/library/RestClient.java | 31 ++++++++++++------- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/github/restclient/MainActivity.java b/app/src/main/java/com/github/restclient/MainActivity.java index a323db7..fdf39ef 100644 --- a/app/src/main/java/com/github/restclient/MainActivity.java +++ b/app/src/main/java/com/github/restclient/MainActivity.java @@ -65,6 +65,8 @@ private void initRestClient() { .setDebugEnable(true) .setHeader(header) .setConnectionTimeOut(15000) + .setWriteTimeOut(15000) + .setReadTimeOut(15000) .build(); } @@ -87,6 +89,8 @@ private void initRestSamlple() { .Builder(this) .setHeader(header) .setConnectionTimeOut(15000) + .setWriteTimeOut(15000) + .setReadTimeOut(15000) .build(); } @@ -122,7 +126,7 @@ private void initRestClientWithCP() { private void callSimpleApi() { RequestParams params = new RequestParams(RequestBodyType.FormData); - restClient.GET("https://api.spotify.com/v1/tracks/2TpxZ7JUBn3uw46aR7qd6V", + restClient.GET("http://nestle.koosha.ir/User/UserLogin.ashx?Username=MahdiPiran&Password=12345678&", "", new ResponseJsonHandler() { @Override diff --git a/build.gradle.kts b/build.gradle.kts index d37c84f..b90cc9a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,8 +6,8 @@ buildscript { extra.set("compileSdk", 28) extra.set("targetSdk", 28) extra.set("buildTools", "28.0.3") - extra.set("version_code", 16) - extra.set("version_name", "1.1.7") + extra.set("version_code", 17) + extra.set("version_name", "1.1.8") repositories { google() @@ -15,7 +15,7 @@ buildscript { maven("https://jitpack.io") } dependencies { - classpath("com.android.tools.build:gradle:3.4.1") + classpath("com.android.tools.build:gradle:3.5.0") classpath("com.github.dcendents:android-maven-gradle-plugin:2.1") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0a5e757..e017b72 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip diff --git a/library/src/main/java/com/github/library/BaseClient.java b/library/src/main/java/com/github/library/BaseClient.java index 3325216..8b8e9d6 100644 --- a/library/src/main/java/com/github/library/BaseClient.java +++ b/library/src/main/java/com/github/library/BaseClient.java @@ -48,20 +48,23 @@ abstract class BaseClient { int timeRead = 30; + int timeWrite = 30; + boolean debugEnable = true; private static OkHttpClient instance; static CertificatePinner certificatePinner; - static synchronized OkHttpClient getClient(int timeOut, int readTime, boolean enableDebug) { + static synchronized OkHttpClient getClient(int timeOut, int readTime,int timeWrite, boolean enableDebug) { if (instance == null) { OkHttpClient.Builder instanceBuilder = new OkHttpClient() .newBuilder() .readTimeout(readTime, TimeUnit.MILLISECONDS) - .connectTimeout(timeOut, TimeUnit.MILLISECONDS); + .connectTimeout(timeOut, TimeUnit.MILLISECONDS) + .writeTimeout(timeWrite,TimeUnit.MILLISECONDS); if (enableDebug) instanceBuilder.addInterceptor(new LoggingInterceptor()); @@ -76,13 +79,13 @@ static synchronized OkHttpClient getClient(int timeOut, int readTime, boolean en } public void cancelAllRequest() { - for (Call call : getClient(timeMilliSecond, timeRead, debugEnable).dispatcher().queuedCalls()) { + for (Call call : getClient(timeMilliSecond, timeRead,timeWrite, debugEnable).dispatcher().queuedCalls()) { call.cancel(); } } public void cancelCallWithTag(String tag) { - for (Call call : getClient(timeMilliSecond, timeRead, debugEnable).dispatcher().queuedCalls()) { + for (Call call : getClient(timeMilliSecond, timeRead,timeWrite, debugEnable).dispatcher().queuedCalls()) { try { Object item = call.request().tag(); if (item != null) { diff --git a/library/src/main/java/com/github/library/RestClient.java b/library/src/main/java/com/github/library/RestClient.java index e22c555..abb2c22 100644 --- a/library/src/main/java/com/github/library/RestClient.java +++ b/library/src/main/java/com/github/library/RestClient.java @@ -40,6 +40,8 @@ public static class Builder { private Integer timeRead = 30; + private Integer timeWrite = 30; + private boolean debugEnable = true; private AuthType authType = AuthType.NO_AUTH; @@ -78,7 +80,12 @@ public Builder setConnectionTimeOut(Integer timeMilliSecond) { } public Builder setReadTimeOut(Integer timeMilliSecond) { - this.timeMilliSecond = timeMilliSecond; + this.timeRead = timeMilliSecond; + return this; + } + + public Builder setWriteTimeOut(Integer timeMilliSecond) { + this.timeWrite = timeMilliSecond; return this; } @@ -114,6 +121,8 @@ private RestClient(Builder builder) { this.password = builder.password; this.debugEnable = builder.debugEnable; this.timeMilliSecond = builder.timeMilliSecond; + this.timeRead = builder.timeRead; + this.timeWrite = builder.timeWrite; this.authType = builder.authType; this.headers = builder.headers; @@ -131,9 +140,9 @@ public void POST( } //endregion if (this.authType == AuthType.OAUTH2_AUTH) { - POST.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + POST.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } else { - POST.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + POST.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } } @@ -150,9 +159,9 @@ public void POST( } //endregion if (this.authType == AuthType.OAUTH2_AUTH) { - POST.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder); + POST.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder); } else { - POST.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder); + POST.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder); } } @@ -167,9 +176,9 @@ public void GET( //endregion if (this.authType == AuthType.OAUTH2_AUTH) { - GET.oauth2_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), responder); + GET.oauth2_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), responder); } else { - GET.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), responder); + GET.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), responder); } } @@ -185,9 +194,9 @@ public void DELETE( } if (this.authType == AuthType.OAUTH2_AUTH) { - DELETE.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + DELETE.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } else { - DELETE.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + DELETE.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } } @@ -203,9 +212,9 @@ public void PUT( } if (this.authType == AuthType.OAUTH2_AUTH) { - PUT.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + PUT.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } else { - PUT.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder); + PUT.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder); } } }