diff --git a/README.md b/README.md index 5ec9b18..522fb77 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Step 2. Add the dependency ``` dependencies { - implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.0' + implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.1' } ``` diff --git a/build.gradle b/build.gradle index ad5dd33..4d09de6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,15 +1,17 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.51' + ext.android_support_version = '28.0.0' + ext.kotlin_version = '1.2.71' ext.anko_version = '0.10.5' + ext.glide_version = '4.7.1' ext.retrofit_version = '2.4.0' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.3' + classpath 'com.android.tools.build:gradle:3.2.1' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4dc5cfe..56d4991 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-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/retrofitLib/build.gradle b/retrofitLib/build.gradle index e401382..8854770 100644 --- a/retrofitLib/build.gradle +++ b/retrofitLib/build.gradle @@ -5,11 +5,11 @@ apply plugin: 'com.github.dcendents.android-maven' group = 'com.github.zeropercenthappy' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { minSdkVersion 19 - targetSdkVersion 27 + targetSdkVersion 28 versionCode 1 versionName "1.0" @@ -29,12 +29,14 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:27.1.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation "org.jetbrains.anko:anko-common:$anko_version" + implementation "com.android.support:appcompat-v7:$android_support_version" + testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation "org.jetbrains.anko:anko-common:$anko_version" + implementation 'com.github.zeropercenthappy:ZPHAndroidUtils:1.1.2' api "com.squareup.retrofit2:retrofit:$retrofit_version" api "com.squareup.retrofit2:converter-gson:$retrofit_version" diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/OkHttpClientBuilder.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/OkHttpClientBuilder.kt index 2bc407c..d53a666 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/OkHttpClientBuilder.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/OkHttpClientBuilder.kt @@ -1,12 +1,14 @@ package com.zeropercenthappy.retrofitutil -import android.content.Context import okhttp3.Interceptor import okhttp3.OkHttpClient -import okhttp3.logging.HttpLoggingInterceptor +import java.util.concurrent.TimeUnit class OkHttpClientBuilder { private val interceptorList = arrayListOf() + private var connectTimeoutMs: Long = 10_000 + private var readTimeoutMs: Long = 10_000 + private var writeTimeoutMs: Long = 10_000 fun addInterceptor(interceptor: Interceptor): OkHttpClientBuilder { interceptorList.add(interceptor) @@ -18,11 +20,29 @@ class OkHttpClientBuilder { return this } + fun connectTimeout(ms: Long): OkHttpClientBuilder { + this.connectTimeoutMs = ms + return this + } + + fun readTimeoutMs(ms: Long): OkHttpClientBuilder { + this.readTimeoutMs = ms + return this + } + + fun writeTimeoutSec(ms: Long): OkHttpClientBuilder { + this.writeTimeoutMs = ms + return this + } + fun build(): OkHttpClient { val builder = OkHttpClient.Builder() for (interceptor in interceptorList) { builder.addInterceptor(interceptor) } + builder.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS) + builder.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS) + builder.writeTimeout(writeTimeoutMs, TimeUnit.MILLISECONDS) return builder.build() } } \ No newline at end of file diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt index dbc9c4a..fa11d96 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt @@ -6,7 +6,6 @@ import okhttp3.Interceptor import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import java.lang.Exception import java.util.* class RetrofitBuilder { @@ -15,6 +14,9 @@ class RetrofitBuilder { private var extraParamMap = TreeMap() private val extraHeaderMap = TreeMap() private val extraInterceptorList = arrayListOf() + private var connectTimeoutMs: Long = 10_000 + private var readTimeoutMs: Long = 10_000 + private var writeTimeoutMs: Long = 10_000 fun baseUrl(baseUrl: String): RetrofitBuilder { this.baseUrl = baseUrl @@ -66,6 +68,21 @@ class RetrofitBuilder { return this } + fun connectTimeout(ms: Long): RetrofitBuilder { + this.connectTimeoutMs = ms + return this + } + + fun readTimeoutMs(ms: Long): RetrofitBuilder { + this.readTimeoutMs = ms + return this + } + + fun writeTimeoutSec(ms: Long): RetrofitBuilder { + this.writeTimeoutMs = ms + return this + } + fun build(context: Context): Retrofit { if (TextUtils.isEmpty(baseUrl)) { throw Exception("base url can not be empty") @@ -83,6 +100,9 @@ class RetrofitBuilder { // 构造OkHttpClient val okHttpClient = OkHttpClientBuilder() .addInterceptors(extraInterceptorList) + .connectTimeout(connectTimeoutMs) + .readTimeoutMs(readTimeoutMs) + .writeTimeoutSec(writeTimeoutMs) .build() return Retrofit.Builder() diff --git a/sample/build.gradle b/sample/build.gradle index fb7da00..11dbafd 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -2,11 +2,11 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { applicationId "com.zeropercenthappy.retrofitutilsample" minSdkVersion 19 - targetSdkVersion 27 + targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -22,15 +22,17 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.anko:anko-common:$anko_version" - implementation 'com.android.support:appcompat-v7:27.1.1' - implementation 'com.android.support:design:27.1.1' - implementation 'com.android.support:cardview-v7:27.1.1' + implementation "com.android.support:appcompat-v7:$android_support_version" + implementation "com.android.support:design:$android_support_version" + implementation "com.android.support:cardview-v7:$android_support_version" + implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation project(':retrofitLib') implementation('com.yanzhenjie:album:2.0.2') { exclude group: 'com.android.support'