From b67ed8dee98ffcb2c5d75a2efe35ea9b6b3192a8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 6 Sep 2019 11:28:59 +0800 Subject: [PATCH] up --- .../io/legado/app/help/http/HttpHelper.kt | 42 +++++++++---------- .../java/io/legado/app/lib/webdav/WebDav.kt | 9 ++-- .../io/legado/app/lib/webdav/http/OkHttp.kt | 7 ---- 3 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 342d0a1e3..5ae7c286e 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -7,27 +7,7 @@ import java.util.concurrent.TimeUnit object HttpHelper { - val client: OkHttpClient = getOkHttpClient() - - inline fun getApiService(baseUrl: String): T { - return getRetrofit(baseUrl).create(T::class.java) - } - - inline fun getApiService(baseUrl: String, encode: String): T { - return getRetrofit(baseUrl, encode).create(T::class.java) - } - - fun getRetrofit(baseUrl: String, encode: String? = null): Retrofit { - return Retrofit.Builder().baseUrl(baseUrl) - //增加返回值为字符串的支持(以实体类返回) - .addConverterFactory(EncodeConverter.create(encode)) - //增加返回值为Observable的支持 - .addCallAdapterFactory(CoroutinesCallAdapterFactory.create()) - .client(client) - .build() - } - - private fun getOkHttpClient(): OkHttpClient { + val client: OkHttpClient by lazy { val default = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() @@ -50,7 +30,25 @@ object HttpHelper { .protocols(listOf(Protocol.HTTP_1_1)) .addInterceptor(getHeaderInterceptor()) - return builder.build() + builder.build() + } + + inline fun getApiService(baseUrl: String): T { + return getRetrofit(baseUrl).create(T::class.java) + } + + inline fun getApiService(baseUrl: String, encode: String): T { + return getRetrofit(baseUrl, encode).create(T::class.java) + } + + fun getRetrofit(baseUrl: String, encode: String? = null): Retrofit { + return Retrofit.Builder().baseUrl(baseUrl) + //增加返回值为字符串的支持(以实体类返回) + .addConverterFactory(EncodeConverter.create(encode)) + //增加返回值为Observable的支持 + .addCallAdapterFactory(CoroutinesCallAdapterFactory.create()) + .client(client) + .build() } private fun getHeaderInterceptor(): Interceptor { diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index f35e41db3..ec1dc573e 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -1,10 +1,13 @@ package io.legado.app.lib.webdav +import io.legado.app.help.http.HttpHelper import io.legado.app.lib.webdav.http.Handler import io.legado.app.lib.webdav.http.HttpAuth -import io.legado.app.lib.webdav.http.OkHttp -import okhttp3.* +import okhttp3.Credentials import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.Request +import okhttp3.RequestBody +import okhttp3.Response import org.jsoup.Jsoup import java.io.File import java.io.IOException @@ -32,7 +35,7 @@ constructor(url: String) { return field } - private val okHttpClient: OkHttpClient = OkHttp.okHttpClient + private val okHttpClient = HttpHelper.client val path: String get() = url.toString() diff --git a/app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt b/app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt deleted file mode 100644 index 6861deb62..000000000 --- a/app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.legado.app.lib.webdav.http - -import okhttp3.OkHttpClient - -object OkHttp { - val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() -} \ No newline at end of file