pull/32/head
kunfei 5 years ago
parent 09320bd133
commit b67ed8dee9
  1. 42
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 9
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  3. 7
      app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt

@ -7,27 +7,7 @@ import java.util.concurrent.TimeUnit
object HttpHelper { object HttpHelper {
val client: OkHttpClient = getOkHttpClient() val client: OkHttpClient by lazy {
inline fun <reified T> getApiService(baseUrl: String): T {
return getRetrofit(baseUrl).create(T::class.java)
}
inline fun <reified T> 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<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
}
private fun getOkHttpClient(): OkHttpClient {
val default = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) val default = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2) .tlsVersions(TlsVersion.TLS_1_2)
.build() .build()
@ -50,7 +30,25 @@ object HttpHelper {
.protocols(listOf(Protocol.HTTP_1_1)) .protocols(listOf(Protocol.HTTP_1_1))
.addInterceptor(getHeaderInterceptor()) .addInterceptor(getHeaderInterceptor())
return builder.build() builder.build()
}
inline fun <reified T> getApiService(baseUrl: String): T {
return getRetrofit(baseUrl).create(T::class.java)
}
inline fun <reified T> 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<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
} }
private fun getHeaderInterceptor(): Interceptor { private fun getHeaderInterceptor(): Interceptor {

@ -1,10 +1,13 @@
package io.legado.app.lib.webdav 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.Handler
import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.lib.webdav.http.HttpAuth
import io.legado.app.lib.webdav.http.OkHttp import okhttp3.Credentials
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -32,7 +35,7 @@ constructor(url: String) {
return field return field
} }
private val okHttpClient: OkHttpClient = OkHttp.okHttpClient private val okHttpClient = HttpHelper.client
val path: String val path: String
get() = url.toString() get() = url.toString()

@ -1,7 +0,0 @@
package io.legado.app.lib.webdav.http
import okhttp3.OkHttpClient
object OkHttp {
val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
}
Loading…
Cancel
Save