From 7ff4f0cb12ad3864c582f65bd53a5a98a0cec60a Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 25 Mar 2020 17:33:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 3 +-- app/proguard-rules.pro | 2 -- .../io/legado/app/help/http/HttpHelper.kt | 18 +++++++-------- .../java/io/legado/app/lib/webdav/WebDav.kt | 22 +++++++++---------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 93e569b2e..f4bb93125 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -164,8 +164,7 @@ dependencies { //JS rhino implementation 'com.github.gedoor:rhino-android:1.4' - //Retrofit - implementation 'com.squareup.okhttp3:logging-interceptor:4.4.1' + //网络 implementation 'com.squareup.retrofit2:retrofit:2.8.0' //Glide diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 9af1203b9..1e8ab731b 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -160,7 +160,6 @@ -dontwarn rx.** -dontwarn okio.** --dontwarn retrofit2.** -dontwarn javax.annotation.** -dontwarn org.apache.log4j.lf5.viewer.** -dontnote org.apache.log4j.lf5.viewer.** @@ -172,7 +171,6 @@ -dontwarn com.jeremyliao.liveeventbus.** -keep class com.jeremyliao.liveeventbus.** { *; } --keep class retrofit2.**{*;} -keep class okhttp3.**{*;} -keep class okio.**{*;} -keep class com.hwangjr.rxbus.**{*;} 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 df417e2f9..31064e268 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 @@ -3,9 +3,11 @@ package io.legado.app.help.http import io.legado.app.help.http.api.HttpGetApi import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.suspendCancellableCoroutine -import okhttp3.* +import okhttp3.ConnectionSpec +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.Protocol import retrofit2.Retrofit -import java.util.* import java.util.concurrent.TimeUnit import kotlin.coroutines.resume @@ -13,14 +15,12 @@ import kotlin.coroutines.resume object HttpHelper { val client: OkHttpClient by lazy { - val default = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) - .tlsVersions(TlsVersion.TLS_1_2) - .build() - val specs = ArrayList() - specs.add(default) - specs.add(ConnectionSpec.COMPATIBLE_TLS) - specs.add(ConnectionSpec.CLEARTEXT) + val specs = arrayListOf( + ConnectionSpec.MODERN_TLS, + ConnectionSpec.COMPATIBLE_TLS, + ConnectionSpec.CLEARTEXT + ) val builder = OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) 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 47aa59344..d1648c7b7 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 @@ -3,12 +3,7 @@ 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 okhttp3.Credentials -import okhttp3.MediaType.Companion.toMediaTypeOrNull -import okhttp3.Request -import okhttp3.RequestBody.Companion.asRequestBody -import okhttp3.RequestBody.Companion.toRequestBody -import okhttp3.Response +import okhttp3.* import org.jsoup.Jsoup import java.io.File import java.io.IOException @@ -82,7 +77,7 @@ constructor(urlStr: String) { this.exists = false return false } - response.body?.let { + response.body()?.let { if (it.string().isNotEmpty()) { return true } @@ -102,7 +97,7 @@ constructor(urlStr: String) { fun listFiles(propsList: ArrayList = ArrayList()): List { propFindResponse(propsList)?.let { response -> if (response.isSuccessful) { - response.body?.let { body -> + response.body()?.let { body -> return parseDir(body.string()) } } @@ -127,7 +122,10 @@ constructor(urlStr: String) { .url(url) // 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 // 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 - .method("PROPFIND", requestPropsStr.toRequestBody("text/plain".toMediaTypeOrNull())) + .method( + "PROPFIND", + RequestBody.create(MediaType.parse("text/plain"), requestPropsStr) + ) HttpAuth.auth?.let { request.header( @@ -205,9 +203,9 @@ constructor(urlStr: String) { fun upload(localPath: String, contentType: String? = null): Boolean { val file = File(localPath) if (!file.exists()) return false - val mediaType = contentType?.toMediaTypeOrNull() + val mediaType = contentType?.let { MediaType.parse(it) } // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 - val fileBody = file.asRequestBody(mediaType) + val fileBody = RequestBody.create(mediaType, file) httpUrl?.let { val request = Request.Builder() .url(it) @@ -241,7 +239,7 @@ constructor(urlStr: String) { request.header("Authorization", Credentials.basic(it.user, it.pass)) } try { - return HttpHelper.client.newCall(request.build()).execute().body?.byteStream() + return HttpHelper.client.newCall(request.build()).execute().body()?.byteStream() } catch (e: IOException) { e.printStackTrace() } catch (e: IllegalArgumentException) {