From 2a4d95f1d405f933a71cf28138f005614be09d2b Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 00:55:07 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E4=B8=BARxHttp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 +- .../java/io/legado/app/help/JsExtensions.kt | 12 +- .../io/legado/app/help/http/AjaxWebView.kt | 12 +- .../io/legado/app/help/http/ByteConverter.kt | 20 --- .../io/legado/app/help/http/CookieStore.kt | 2 +- .../legado/app/help/http/EncodeConverter.kt | 40 ------ .../io/legado/app/help/http/HttpHelper.kt | 31 +--- .../main/java/io/legado/app/help/http/Res.kt | 6 - .../io/legado/app/help/http/StrResponse.kt | 124 ++++++++++++++++ .../io/legado/app/help/http/api/HttpGetApi.kt | 67 --------- .../legado/app/help/http/api/HttpPostApi.kt | 60 -------- .../legado/app/help/http/parser/ByteParser.kt | 2 +- .../app/help/http/parser/InputStreamParser.kt | 2 +- .../help/http/parser/SimpleResponseParser.kt | 38 +++++ .../legado/app/help/http/parser/TextParser.kt | 2 +- .../java/io/legado/app/lib/webdav/WebDav.kt | 15 +- .../app/model/analyzeRule/AnalyzeRule.kt | 2 +- .../app/model/analyzeRule/AnalyzeUrl.kt | 132 ++++++------------ .../main/java/io/legado/app/model/rss/Rss.kt | 4 +- .../app/model/webBook/BookChapterList.kt | 4 +- .../legado/app/model/webBook/BookContent.kt | 4 +- .../io/legado/app/model/webBook/WebBook.kt | 10 +- .../ui/book/explore/ExploreShowViewModel.kt | 3 + .../java/io/legado/app/utils/NetworkUtils.kt | 8 -- 24 files changed, 253 insertions(+), 353 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/help/http/ByteConverter.kt delete mode 100644 app/src/main/java/io/legado/app/help/http/EncodeConverter.kt delete mode 100644 app/src/main/java/io/legado/app/help/http/Res.kt create mode 100644 app/src/main/java/io/legado/app/help/http/StrResponse.kt delete mode 100644 app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt delete mode 100644 app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt create mode 100644 app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt diff --git a/app/build.gradle b/app/build.gradle index f5baacd8e..d43b69821 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -172,10 +172,10 @@ dependencies { implementation 'com.github.gedoor:rhino-android:1.4' //网络 - implementation 'com.ljx.rxhttp:rxhttp:2.4.4' - implementation 'com.squareup.retrofit2:retrofit:2.9.0' + implementation 'com.ljx.rxhttp:rxhttp:2.4.4-beta2' + implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1' - kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.4' + kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.4-beta2' //Glide implementation 'com.github.bumptech.glide:glide:4.11.0' diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 9629e3efe..825141793 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -31,9 +31,9 @@ interface JsExtensions { fun ajax(urlStr: String): String? { return try { val analyzeUrl = AnalyzeUrl(urlStr) - val call = analyzeUrl.getResponse(urlStr) - val response = call.execute() - response.body() + runBlocking { + analyzeUrl.getStrResponse(urlStr).body + } } catch (e: Exception) { e.msg } @@ -45,9 +45,9 @@ interface JsExtensions { fun connect(urlStr: String): Any { return try { val analyzeUrl = AnalyzeUrl(urlStr) - val call = analyzeUrl.getResponse(urlStr) - val response = call.execute() - response + runBlocking { + analyzeUrl.getStrResponse(urlStr) + } } catch (e: Exception) { e.msg } diff --git a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt index db6dc165b..c14423c8b 100644 --- a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt @@ -39,7 +39,7 @@ class AjaxWebView { mWebView = createAjaxWebView(params, this) } MSG_SUCCESS -> { - ajaxWebView.callback?.onResult(msg.obj as Res) + ajaxWebView.callback?.onResult(msg.obj as StrResponse) destroyWebView() } MSG_ERROR -> { @@ -160,7 +160,7 @@ class AjaxWebView { if (it.isNotEmpty() && it != "null") { val content = StringEscapeUtils.unescapeJson(it) .replace("^\"|\"$".toRegex(), "") - handler.obtainMessage(MSG_SUCCESS, Res(url, content)) + handler.obtainMessage(MSG_SUCCESS, StrResponse.success(content, url)) .sendToTarget() handler.removeCallbacks(this) return@evaluateJavascript @@ -186,8 +186,10 @@ class AjaxWebView { override fun onLoadResource(view: WebView, url: String) { params.sourceRegex?.let { if (url.matches(it.toRegex())) { - handler.obtainMessage(MSG_SUCCESS, Res(view.url ?: params.url, url)) - .sendToTarget() + handler.obtainMessage( + MSG_SUCCESS, + StrResponse.success(url, view.url ?: params.url) + ).sendToTarget() } } } @@ -226,7 +228,7 @@ class AjaxWebView { } abstract class Callback { - abstract fun onResult(response: Res) + abstract fun onResult(response: StrResponse) abstract fun onError(error: Throwable) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/ByteConverter.kt b/app/src/main/java/io/legado/app/help/http/ByteConverter.kt deleted file mode 100644 index bb42ec3a8..000000000 --- a/app/src/main/java/io/legado/app/help/http/ByteConverter.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.legado.app.help.http - -import okhttp3.ResponseBody -import retrofit2.Converter -import retrofit2.Retrofit -import java.lang.reflect.Type - -class ByteConverter : Converter.Factory() { - - override fun responseBodyConverter( - type: Type?, - annotations: Array?, - retrofit: Retrofit? - ): Converter? { - return Converter { value -> - value.bytes() - } - } - -} diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 55b5bb141..8c4aa34ec 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -108,6 +108,6 @@ object CookieStore : CookiePersistor, CookieManager { } private fun createCookieKey(cookie: okhttp3.Cookie): String { - return (if (cookie.secure()) "https" else "http") + "://" + cookie.domain() + cookie.path() + "|" + cookie.name() + return (if (cookie.secure) "https" else "http") + "://" + cookie.domain + cookie.path + "|" + cookie.name } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/EncodeConverter.kt b/app/src/main/java/io/legado/app/help/http/EncodeConverter.kt deleted file mode 100644 index 0cad990f0..000000000 --- a/app/src/main/java/io/legado/app/help/http/EncodeConverter.kt +++ /dev/null @@ -1,40 +0,0 @@ -package io.legado.app.help.http - -import io.legado.app.utils.EncodingDetect -import io.legado.app.utils.UTF8BOMFighter -import okhttp3.ResponseBody -import retrofit2.Converter -import retrofit2.Retrofit -import java.lang.reflect.Type -import java.nio.charset.Charset - -class EncodeConverter(private val encode: String? = null) : Converter.Factory() { - - override fun responseBodyConverter( - type: Type?, - annotations: Array?, - retrofit: Retrofit? - ): Converter? { - return Converter { value -> - val responseBytes = UTF8BOMFighter.removeUTF8BOM(value.bytes()) - var charsetName: String? = encode - - charsetName?.let { - try { - return@Converter String(responseBytes, Charset.forName(charsetName)) - } catch (e: Exception) { - } - } - - //根据http头判断 - value.contentType()?.charset()?.let { - return@Converter String(responseBytes, it) - } - - //根据内容判断 - charsetName = EncodingDetect.getHtmlEncode(responseBytes) - String(responseBytes, Charset.forName(charsetName)) - } - } - -} 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 fdd9e20fe..687057e6b 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 @@ -1,8 +1,8 @@ package io.legado.app.help.http +import io.legado.app.utils.msg import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.* -import retrofit2.Retrofit import java.io.IOException import java.net.InetSocketAddress import java.net.Proxy @@ -55,26 +55,6 @@ object HttpHelper { }) } - inline fun getApiService( - baseUrl: String, - encode: String? = null, - proxy: String? = null - ): T { - return getRetrofit(baseUrl, encode, proxy).create(T::class.java) - } - - fun getRetrofit( - baseUrl: String, - encode: String? = null, - proxy: String? = null - ): Retrofit { - return Retrofit.Builder().baseUrl(baseUrl) - //增加返回值为字符串的支持(以实体类返回) - .addConverterFactory(EncodeConverter(encode)) - .client(getProxyClient(proxy)) - .build() - } - fun getProxyClient(proxy: String? = null): OkHttpClient { if (proxy.isNullOrBlank()) { return client @@ -108,7 +88,7 @@ object HttpHelper { if (username != "" && password != "") { builder.proxyAuthenticator { _, response -> //设置代理服务器账号密码 val credential: String = Credentials.basic(username, password) - response.request().newBuilder() + response.request.newBuilder() .header("Proxy-Authorization", credential) .build() } @@ -130,21 +110,22 @@ object HttpHelper { } } - suspend fun ajax(params: AjaxWebView.AjaxParams): Res = + suspend fun ajax(params: AjaxWebView.AjaxParams): StrResponse = suspendCancellableCoroutine { block -> val webView = AjaxWebView() block.invokeOnCancellation { webView.destroyWebView() } webView.callback = object : AjaxWebView.Callback() { - override fun onResult(response: Res) { + override fun onResult(response: StrResponse) { + if (!block.isCompleted) block.resume(response) } override fun onError(error: Throwable) { if (!block.isCompleted) - block.resume(Res(params.url, error.localizedMessage)) + block.resume(StrResponse.success(error.msg, params.url)) } } webView.load(params) diff --git a/app/src/main/java/io/legado/app/help/http/Res.kt b/app/src/main/java/io/legado/app/help/http/Res.kt deleted file mode 100644 index e54832b40..000000000 --- a/app/src/main/java/io/legado/app/help/http/Res.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.legado.app.help.http - -data class Res( - val url: String, - val body: String?, -) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/StrResponse.kt b/app/src/main/java/io/legado/app/help/http/StrResponse.kt new file mode 100644 index 000000000..823a0ba30 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/StrResponse.kt @@ -0,0 +1,124 @@ +package io.legado.app.help.http + +import okhttp3.* +import okhttp3.Response.Builder +import java.util.* + +/** + * An HTTP response. + */ +@Suppress("unused", "MemberVisibilityCanBePrivate") +class StrResponse private constructor( + val rawResponse: Response, + val body: String?, + val errorBody: ResponseBody? +) { + val raw get() = rawResponse + + val url: String + get() { + raw.networkResponse?.let { + return it.request.url.toString() + } + return raw.request.url.toString() + } + + fun code(): Int { + return rawResponse.code + } + + fun message(): String { + return rawResponse.message + } + + fun headers(): Headers { + return rawResponse.headers + } + + val isSuccessful: Boolean + get() = rawResponse.isSuccessful + + fun errorBody(): ResponseBody? { + return errorBody + } + + override fun toString(): String { + return rawResponse.toString() + } + + companion object { + fun success(code: Int, body: String): StrResponse { + require(!(code < 200 || code >= 300)) { "code < 200 or >= 300: $code" } + return success( + body, + Builder() // + .code(code) + .message("Response.success()") + .protocol(Protocol.HTTP_1_1) + .request(Request.Builder().url("http://localhost/").build()) + .build() + ) + } + + fun success(body: String, headers: Headers): StrResponse { + return success( + body, + Builder() // + .code(200) + .message("OK") + .protocol(Protocol.HTTP_1_1) + .headers(headers) + .request(Request.Builder().url("http://localhost/").build()) + .build() + ) + } + + fun success(body: String, url: String): StrResponse { + return success( + body, + Builder() // + .code(200) + .message("OK") + .protocol(Protocol.HTTP_1_1) + .request(Request.Builder().url(url).build()) + .build() + ) + } + + @JvmOverloads + fun success( + body: String?, rawResponse: Response = + Builder() // + .code(200) + .message("OK") + .protocol(Protocol.HTTP_1_1) + .request(Request.Builder().url("http://localhost/").build()) + .build() + ): StrResponse { + Objects.requireNonNull(rawResponse, "rawResponse == null") + require(rawResponse.isSuccessful) { "rawResponse must be successful response" } + return StrResponse(rawResponse, body, null) + } + + fun error(code: Int, body: ResponseBody?): StrResponse { + Objects.requireNonNull(body, "body == null") + require(code >= 400) { "code < 400: $code" } + return error( + body, + Builder() // + .code(code) + .message("Response.error()") + .protocol(Protocol.HTTP_1_1) + .request(Request.Builder().url("http://localhost/").build()) + .build() + ) + } + + fun error(body: ResponseBody?, rawResponse: Response): StrResponse { + Objects.requireNonNull(body, "body == null") + Objects.requireNonNull(rawResponse, "rawResponse == null") + require(!rawResponse.isSuccessful) { "rawResponse should not be successful response" } + return StrResponse(rawResponse, null, body) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt b/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt deleted file mode 100644 index 7138c8c20..000000000 --- a/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt +++ /dev/null @@ -1,67 +0,0 @@ -package io.legado.app.help.http.api - -import retrofit2.Call -import retrofit2.Response -import retrofit2.http.GET -import retrofit2.http.HeaderMap -import retrofit2.http.QueryMap -import retrofit2.http.Url - -/** - * Created by GKF on 2018/1/21. - * get web content - */ -@Suppress("unused") -interface HttpGetApi { - @GET - suspend fun getAsync( - @Url url: String, - @HeaderMap headers: Map - ): Response - - @GET - suspend fun getMapAsync( - @Url url: String, - @QueryMap(encoded = true) queryMap: Map, - @HeaderMap headers: Map - ): Response - - @GET - fun get( - @Url url: String, - @HeaderMap headers: Map - ): Call - - @GET - fun getByte( - @Url url: String, - @HeaderMap headers: Map - ): Call - - @GET - fun getMap( - @Url url: String, - @QueryMap(encoded = true) queryMap: Map, - @HeaderMap headers: Map - ): Call - - @GET - fun getMapByte( - @Url url: String, - @QueryMap(encoded = true) queryMap: Map, - @HeaderMap headers: Map - ): Call - - @GET - suspend fun getByteAsync( - @Url url: String, - @HeaderMap headers: Map - ): Response - - @GET - suspend fun getMapByteAsync( - @Url url: String, - @QueryMap(encoded = true) queryMap: Map, - @HeaderMap headers: Map - ): Response -} diff --git a/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt b/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt deleted file mode 100644 index 170894390..000000000 --- a/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt +++ /dev/null @@ -1,60 +0,0 @@ -package io.legado.app.help.http.api - -import okhttp3.RequestBody -import retrofit2.Call -import retrofit2.Response -import retrofit2.http.* - -/** - * Created by GKF on 2018/1/29. - * post - */ -@Suppress("unused") -interface HttpPostApi { - - @FormUrlEncoded - @POST - suspend fun postMapAsync( - @Url url: String, - @FieldMap(encoded = true) fieldMap: Map, - @HeaderMap headers: Map - ): Response - - @POST - suspend fun postBodyAsync( - @Url url: String, - @Body body: RequestBody, - @HeaderMap headers: Map - ): Response - - @FormUrlEncoded - @POST - fun postMap( - @Url url: String, - @FieldMap(encoded = true) fieldMap: Map, - @HeaderMap headers: Map - ): Call - - @POST - fun postBody( - @Url url: String, - @Body body: RequestBody, - @HeaderMap headers: Map - ): Call - - @FormUrlEncoded - @POST - suspend fun postMapByteAsync( - @Url url: String, - @FieldMap(encoded = true) fieldMap: Map, - @HeaderMap headers: Map - ): Response - - @POST - suspend fun postBodyByteAsync( - @Url url: String, - @Body body: RequestBody, - @HeaderMap headers: Map - ): Response - -} diff --git a/app/src/main/java/io/legado/app/help/http/parser/ByteParser.kt b/app/src/main/java/io/legado/app/help/http/parser/ByteParser.kt index 2419daeb0..b2812d41e 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/ByteParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/ByteParser.kt @@ -7,7 +7,7 @@ import rxhttp.wrapper.annotation.Parser class ByteParser : rxhttp.wrapper.parse.Parser { override fun onParse(response: Response): ByteArray { - return response.body()!!.bytes() + return response.body!!.bytes() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/InputStreamParser.kt b/app/src/main/java/io/legado/app/help/http/parser/InputStreamParser.kt index 0b4d61b2e..9ca0ffe4d 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/InputStreamParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/InputStreamParser.kt @@ -8,7 +8,7 @@ import java.io.InputStream class InputStreamParser : rxhttp.wrapper.parse.Parser { override fun onParse(response: Response): InputStream { - return response.body()!!.byteStream() + return response.body!!.byteStream() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt b/app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt new file mode 100644 index 000000000..17fb1b181 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt @@ -0,0 +1,38 @@ +package io.legado.app.help.http.parser + +import io.legado.app.help.http.StrResponse +import io.legado.app.utils.EncodingDetect +import io.legado.app.utils.UTF8BOMFighter +import okhttp3.Response +import rxhttp.wrapper.annotation.Parser +import rxhttp.wrapper.exception.HttpStatusCodeException +import java.nio.charset.Charset + +@Parser(name = "StrResponse") +class StrResponseParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser { + + override fun onParse(response: Response): StrResponse { + return StrResponse.success(getString(response), response) + } + + private fun getString(response: Response): String { + + val responseBody = response.body ?: throw HttpStatusCodeException(response, "内容为空") + val responseBytes = UTF8BOMFighter.removeUTF8BOM(responseBody.bytes()) + var charsetName: String? = encode + + charsetName?.let { + return String(responseBytes, Charset.forName(charsetName)) + } + + //根据http头判断 + responseBody.contentType()?.charset()?.let { + return String(responseBytes, it) + } + + //根据内容判断 + charsetName = EncodingDetect.getHtmlEncode(responseBytes) + return String(responseBytes, Charset.forName(charsetName)) + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt b/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt index 71ec1cece..09891944d 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt @@ -12,7 +12,7 @@ class TextParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser = ArrayList()): List { propFindResponse(propsList)?.let { response -> if (response.isSuccessful) { - response.body()?.let { body -> + response.body?.let { body -> @Suppress("BlockingMethodInNonBlockingContext") return parseDir(body.string()) } @@ -107,7 +110,7 @@ class WebDav(urlStr: String) { httpUrl?.let { url -> // 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 // 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 - val requestBody = RequestBody.create(MediaType.parse("text/plain"), requestPropsStr) + val requestBody = requestPropsStr.toRequestBody("text/plain".toMediaType()) val request = Request.Builder() .url(url) .method("PROPFIND", requestBody) @@ -197,9 +200,8 @@ class WebDav(urlStr: String) { suspend fun upload(localPath: String, contentType: String? = null): Boolean { val file = File(localPath) if (!file.exists()) return false - val mediaType = contentType?.let { MediaType.parse(it) } // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 - val fileBody = RequestBody.create(mediaType, file) + val fileBody = file.asRequestBody(contentType?.toMediaType()) httpUrl?.let { val request = Request.Builder() .url(it) @@ -210,9 +212,8 @@ class WebDav(urlStr: String) { } suspend fun upload(byteArray: ByteArray, contentType: String? = null): Boolean { - val mediaType = contentType?.let { MediaType.parse(it) } // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 - val fileBody = RequestBody.create(mediaType, byteArray) + val fileBody = byteArray.toRequestBody(contentType?.toMediaType()) httpUrl?.let { val request = Request.Builder() .url(it) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 1877edf9d..bf812256e 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -647,7 +647,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { return try { val analyzeUrl = AnalyzeUrl(urlStr, book = book) runBlocking { - analyzeUrl.getRes(urlStr).body + analyzeUrl.getStrResponse(urlStr).body } } catch (e: Exception) { e.localizedMessage diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 86910724a..6f962b421 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -15,15 +15,10 @@ import io.legado.app.help.AppConfig import io.legado.app.help.CacheManager import io.legado.app.help.JsExtensions import io.legado.app.help.http.* -import io.legado.app.help.http.api.HttpGetApi -import io.legado.app.help.http.api.HttpPostApi import io.legado.app.utils.* -import okhttp3.FormBody -import okhttp3.MediaType -import okhttp3.RequestBody -import retrofit2.Call import rxhttp.wrapper.param.RxHttp import rxhttp.wrapper.param.toByteArray +import rxhttp.wrapper.param.toStrResponse import java.net.URLEncoder import java.util.* import java.util.regex.Pattern @@ -50,7 +45,6 @@ class AnalyzeUrl( companion object { val splitUrlRegex = Regex(",\\s*(?=\\{)") private val pagePattern = Pattern.compile("<(.*?)>") - private val jsonType = MediaType.parse("application/json; charset=utf-8") } var url: String = "" @@ -61,7 +55,6 @@ class AnalyzeUrl( private var queryStr: String? = null private val fieldMap = LinkedHashMap() private var charset: String? = null - private var requestBody: RequestBody? = null private var method = RequestMethod.GET private var proxy: String? = null @@ -172,7 +165,7 @@ class AnalyzeUrl( */ private fun initUrl() { var urlArray = ruleUrl.split(splitUrlRegex, 2) - url = urlArray[0] + url = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0])!! urlHasQuery = urlArray[0] NetworkUtils.getBaseUrl(url)?.let { baseUrl = it @@ -223,13 +216,9 @@ class AnalyzeUrl( } RequestMethod.POST -> { body?.let { - if (it.isJson()) { - requestBody = RequestBody.create(jsonType, it) - } else { + if (!it.isJson()) { analyzeFields(it) } - } ?: let { - requestBody = FormBody.Builder().build() } } } @@ -285,56 +274,15 @@ class AnalyzeUrl( return book?.variableMap?.get(key) ?: "" } - fun getResponse(tag: String): Call { - val cookie = CookieStore.getCookie(tag) - if (cookie.isNotEmpty()) { - val cookieMap = CookieStore.cookieToMap(cookie) - val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") - cookieMap.putAll(customCookieMap) - val newCookie = CookieStore.mapToCookie(cookieMap) - newCookie?.let { - headerMap.put("Cookie", it) - } - } - return when { - method == RequestMethod.POST -> { - if (fieldMap.isNotEmpty()) { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .postMap(url, fieldMap, headerMap) - } else { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .postBody(url, requestBody!!, headerMap) - } - } - fieldMap.isEmpty() -> HttpHelper - .getApiService(baseUrl, charset, proxy) - .get(url, headerMap) - else -> HttpHelper - .getApiService(baseUrl, charset, proxy) - .getMap(url, fieldMap, headerMap) - } - } - - suspend fun getRes( + suspend fun getStrResponse( tag: String, jsStr: String? = null, sourceRegex: String? = null, - ): Res { + ): StrResponse { if (type != null) { - return Res(url, StringUtils.byteToHexString(getByteArray(tag))) - } - val cookie = CookieStore.getCookie(tag) - if (cookie.isNotEmpty()) { - val cookieMap = CookieStore.cookieToMap(cookie) - val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") - cookieMap.putAll(customCookieMap) - val newCookie = CookieStore.mapToCookie(cookieMap) - newCookie?.let { - headerMap.put("Cookie", it) - } + return StrResponse.success(StringUtils.byteToHexString(getByteArray(tag)), url) } + setCookie(tag) if (useWebView) { val params = AjaxWebView.AjaxParams(url) params.headerMap = headerMap @@ -345,65 +293,69 @@ class AnalyzeUrl( params.tag = tag return HttpHelper.ajax(params) } - val res = when { - method == RequestMethod.POST -> { + return when (method) { + RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .postMapAsync(url, fieldMap, headerMap) + RxHttp.postForm(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) + .addAll(fieldMap) + .addAllHeader(headerMap) + .toStrResponse().await() } else { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .postBodyAsync(url, requestBody!!, headerMap) + RxHttp.postJson(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) + .addAll(body) + .addAllHeader(headerMap) + .toStrResponse().await() } } - fieldMap.isEmpty() -> { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .getAsync(url, headerMap) - } - else -> { - HttpHelper - .getApiService(baseUrl, charset, proxy) - .getMapAsync(url, fieldMap, headerMap) - } + else -> RxHttp.get(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) + .addAll(fieldMap) + .toStrResponse().await() } - return Res(NetworkUtils.getUrl(res), res.body()) } suspend fun getByteArray(tag: String? = null): ByteArray { - if (tag != null) { - val cookie = CookieStore.getCookie(tag) - if (cookie.isNotEmpty()) { - val cookieMap = CookieStore.cookieToMap(cookie) - val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") - cookieMap.putAll(customCookieMap) - val newCookie = CookieStore.mapToCookie(cookieMap) - newCookie?.let { - headerMap.put("Cookie", it) - } - } - } + setCookie(tag) return when (method) { RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { RxHttp.postForm(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) .addAll(fieldMap) .addAllHeader(headerMap) .toByteArray().await() } else { RxHttp.postJson(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) .addAll(body) .addAllHeader(headerMap) .toByteArray().await() } } else -> RxHttp.get(url) + .setOkClient(HttpHelper.getProxyClient(proxy)) .addAll(fieldMap) .toByteArray().await() } } + private fun setCookie(tag: String?) { + if (tag != null) { + val cookie = CookieStore.getCookie(tag) + if (cookie.isNotEmpty()) { + val cookieMap = CookieStore.cookieToMap(cookie) + val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") + cookieMap.putAll(customCookieMap) + val newCookie = CookieStore.mapToCookie(cookieMap) + newCookie?.let { + headerMap.put("Cookie", it) + } + } + } + } + fun getGlideUrl(): GlideUrl { val headers = LazyHeaders.Builder() headerMap.forEach { (key, value) -> diff --git a/app/src/main/java/io/legado/app/model/rss/Rss.kt b/app/src/main/java/io/legado/app/model/rss/Rss.kt index 979d2e308..a757aed5f 100644 --- a/app/src/main/java/io/legado/app/model/rss/Rss.kt +++ b/app/src/main/java/io/legado/app/model/rss/Rss.kt @@ -26,7 +26,7 @@ object Rss { page = page, headerMapF = rssSource.getHeaderMap() ) - val body = analyzeUrl.getRes(rssSource.sourceUrl).body + val body = analyzeUrl.getStrResponse(rssSource.sourceUrl).body RssParserByRule.parseXML(sortName, sortUrl, body, rssSource) } } @@ -42,7 +42,7 @@ object Rss { val body = AnalyzeUrl( rssArticle.link, baseUrl = rssArticle.origin, headerMapF = rssSource?.getHeaderMap() - ).getRes(rssArticle.origin) + ).getStrResponse(rssArticle.origin) .body val analyzeRule = AnalyzeRule() analyzeRule.setContent(body) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index fb51b4802..94ba0baf7 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -63,7 +63,7 @@ object BookChapterList { ruleUrl = nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getRes(bookSource.bookSourceUrl) + ).getStrResponse(bookSource.bookSourceUrl) .body?.let { nextBody -> chapterData = analyzeChapterList( book, nextUrl, nextBody, tocRule, listRule, bookSource @@ -132,7 +132,7 @@ object BookChapterList { ruleUrl = chapterData.nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getRes(bookSource.bookSourceUrl).body + ).getStrResponse(bookSource.bookSourceUrl).body ?: throw Exception("${chapterData.nextUrl}, 下载失败") val nextChapterData = analyzeChapterList( book, chapterData.nextUrl, nextBody, tocRule, listRule, bookSource, diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index d3d055ffa..f1bb8d405 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -56,7 +56,7 @@ object BookContent { ruleUrl = nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getRes(bookSource.bookSourceUrl).body?.let { nextBody -> + ).getStrResponse(bookSource.bookSourceUrl).body?.let { nextBody -> contentData = analyzeContent( book, nextUrl, nextBody, contentRule, bookChapter, bookSource, false ) @@ -78,7 +78,7 @@ object BookContent { ruleUrl = item.nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getRes(bookSource.bookSourceUrl).body?.let { + ).getStrResponse(bookSource.bookSourceUrl).body?.let { contentData = analyzeContent( book, item.nextUrl, it, contentRule, bookChapter, bookSource, false ) diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index b7057ee61..7a1c29a05 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -46,7 +46,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap(), book = variableBook ) - val res = analyzeUrl.getRes(bookSource.bookSourceUrl) + val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) return BookList.analyzeBookList( scope, res.body, @@ -77,7 +77,7 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - val res = analyzeUrl.getRes(bookSource.bookSourceUrl) + val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) BookList.analyzeBookList( scope, res.body, @@ -110,7 +110,7 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap(), book = book - ).getRes(bookSource.bookSourceUrl) + ).getStrResponse(bookSource.bookSourceUrl) BookInfo.analyzeBookInfo(book, res.body, bookSource, book.bookUrl, canReName) } book @@ -141,7 +141,7 @@ class WebBook(val bookSource: BookSource) { ruleUrl = book.tocUrl, baseUrl = book.bookUrl, headerMapF = bookSource.getHeaderMap() - ).getRes(bookSource.bookSourceUrl) + ).getStrResponse(bookSource.bookSourceUrl) BookChapterList.analyzeChapterList( this, book, @@ -201,7 +201,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap(), book = book, chapter = bookChapter - ).getRes( + ).getStrResponse( bookSource.bookSourceUrl, jsStr = bookSource.getContentRule().webJs, sourceRegex = bookSource.getContentRule().sourceRegex diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index 4473d00ca..f4e3e40be 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt @@ -8,6 +8,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.model.webBook.WebBook +import io.legado.app.utils.msg import kotlinx.coroutines.Dispatchers.IO class ExploreShowViewModel(application: Application) : BaseViewModel(application) { @@ -39,6 +40,8 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application booksData.postValue(searchBooks) App.db.searchBookDao.insert(*searchBooks.toTypedArray()) page++ + }.onError { + toast(it.msg) } } } diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index d5464c6aa..79dcb5790 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -1,6 +1,5 @@ package io.legado.app.utils -import retrofit2.Response import java.net.InetAddress import java.net.NetworkInterface import java.net.SocketException @@ -11,13 +10,6 @@ import java.util.regex.Pattern @Suppress("unused", "MemberVisibilityCanBePrivate") object NetworkUtils { - fun getUrl(response: Response<*>): String { - response.raw().networkResponse()?.let { - return it.request().url().toString() - } - return response.raw().request().url().toString() - } - private val notNeedEncoding: BitSet by lazy { val bitSet = BitSet(256) for (i in 'a'.toInt()..'z'.toInt()) { From 3bfeedae68fa38f894d5ebf3e58d11ffb2ac7be0 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 09:30:07 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http/parser/{SimpleResponseParser.kt => StrResponseParser.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/io/legado/app/help/http/parser/{SimpleResponseParser.kt => StrResponseParser.kt} (100%) diff --git a/app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt similarity index 100% rename from app/src/main/java/io/legado/app/help/http/parser/SimpleResponseParser.kt rename to app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt From 54f12baf0f77a599f5468393a77b48f62e0a8a2c Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 09:40:03 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/http/parser/StrResponseParser.kt | 3 ++- .../main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 1 + .../java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt index 17fb1b181..53ead9d26 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt @@ -12,7 +12,8 @@ import java.nio.charset.Charset class StrResponseParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser { override fun onParse(response: Response): StrResponse { - return StrResponse.success(getString(response), response) + val body = getString(response) + return StrResponse.success(body, response) } private fun getString(response: Response): String { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 6f962b421..e36750a1f 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -312,6 +312,7 @@ class AnalyzeUrl( else -> RxHttp.get(url) .setOkClient(HttpHelper.getProxyClient(proxy)) .addAll(fieldMap) + .addAllHeader(headerMap) .toStrResponse().await() } } diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index f4e3e40be..089d0d2fb 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt @@ -41,6 +41,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application App.db.searchBookDao.insert(*searchBooks.toTypedArray()) page++ }.onError { + it.printStackTrace() toast(it.msg) } } From 8d45c9e573907638dad803dd51b98a96188a68f0 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 09:54:07 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/entities/BookChapter.kt | 2 +- .../io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 2 +- .../io/legado/app/model/rss/RssParserByRule.kt | 2 +- .../io/legado/app/ui/book/cache/CacheViewModel.kt | 5 ++--- .../ui/book/read/page/provider/ChapterProvider.kt | 15 +++++++-------- .../main/java/io/legado/app/utils/NetworkUtils.kt | 5 ++--- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 4d2e1e9e3..ea97c94f8 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -63,7 +63,7 @@ data class BookChapter( fun getAbsoluteURL(): String { val urlArray = url.split(AnalyzeUrl.splitUrlRegex) - var absoluteUrl = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0])!! + var absoluteUrl = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0]) if (urlArray.size > 1) { absoluteUrl = "$absoluteUrl,${urlArray[1]}" } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index e36750a1f..3e1f6079f 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -165,7 +165,7 @@ class AnalyzeUrl( */ private fun initUrl() { var urlArray = ruleUrl.split(splitUrlRegex, 2) - url = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0])!! + url = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0]) urlHasQuery = urlArray[0] NetworkUtils.getBaseUrl(url)?.let { baseUrl = it diff --git a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt index 955df439b..13503e510 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt @@ -112,7 +112,7 @@ object RssParserByRule { rssArticle.image = analyzeRule.getString(ruleImage, true) Debug.log(sourceUrl, "└${rssArticle.image}", log) Debug.log(sourceUrl, "┌获取文章链接", log) - rssArticle.link = NetworkUtils.getAbsoluteURL(sourceUrl, analyzeRule.getString(ruleLink))!! + rssArticle.link = NetworkUtils.getAbsoluteURL(sourceUrl, analyzeRule.getString(ruleLink)) Debug.log(sourceUrl, "└${rssArticle.link}", log) if (rssArticle.title.isBlank()) { return null diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index d0fd4f34a..2df117411 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -109,9 +109,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { content.split("\n").forEachIndexed { index, text -> val matcher = AppPattern.imgPattern.matcher(text) if (matcher.find()) { - var src = matcher.group(1) - src = NetworkUtils.getAbsoluteURL(chapter.url, src) - src?.let { + matcher.group(1)?.let { + val src = NetworkUtils.getAbsoluteURL(chapter.url, it) srcList.add(Triple(chapter.title, index, src)) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index f81ddfffb..399666b14 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -95,14 +95,13 @@ object ChapterProvider { contents.forEachIndexed { index, text -> val matcher = AppPattern.imgPattern.matcher(text) if (matcher.find()) { - var src = matcher.group(1) - if (!book.isEpub()) { - src = NetworkUtils.getAbsoluteURL(bookChapter.url, src) - } - src?.let { - durY = setTypeImage( - book, bookChapter, src, durY, textPages, imageStyle - ) + matcher.group(1)?.let { + if (!book.isEpub()) { + val src = NetworkUtils.getAbsoluteURL(bookChapter.url, it) + durY = setTypeImage( + book, bookChapter, src, durY, textPages, imageStyle + ) + } } } else { val isTitle = index == 0 diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index 79dcb5790..87be05e05 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -69,9 +69,8 @@ object NetworkUtils { /** * 获取绝对地址 */ - fun getAbsoluteURL(baseURL: String?, relativePath: String?): String? { + fun getAbsoluteURL(baseURL: String?, relativePath: String): String { if (baseURL.isNullOrEmpty()) return relativePath - if (relativePath.isNullOrEmpty()) return baseURL if (relativePath.isAbsUrl()) return relativePath var relativeUrl = relativePath try { @@ -88,7 +87,7 @@ object NetworkUtils { /** * 获取绝对地址 */ - fun getAbsoluteURL(baseURL: URL?, relativePath: String): String? { + fun getAbsoluteURL(baseURL: URL?, relativePath: String): String { if (baseURL == null) return relativePath var relativeUrl = relativePath try { From 307c80b478d12d5016f642915eab07ab6ba93a74 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 11:03:22 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 76137cfa7..92c3e2277 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -97,6 +97,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.loadContent(resetPageOffset = true) } } + if (!BaseReadAloudService.isRun) { + syncBookProgress(book) + } } } From 6dda59a6bc217f7e0b89511c69c62aeb4c05df71 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 11:22:00 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 ++-- .../java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d43b69821..2403cd321 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -172,10 +172,10 @@ dependencies { implementation 'com.github.gedoor:rhino-android:1.4' //网络 - implementation 'com.ljx.rxhttp:rxhttp:2.4.4-beta2' + implementation 'com.ljx.rxhttp:rxhttp:2.4.4-beta3' implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1' - kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.4-beta2' + kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.4-beta3' //Glide implementation 'com.github.bumptech.glide:glide:4.11.0' diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 3e1f6079f..f89af656b 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -298,7 +298,7 @@ class AnalyzeUrl( if (fieldMap.isNotEmpty()) { RxHttp.postForm(url) .setOkClient(HttpHelper.getProxyClient(proxy)) - .addAll(fieldMap) + .addAllEncoded(fieldMap) .addAllHeader(headerMap) .toStrResponse().await() } else { @@ -311,7 +311,7 @@ class AnalyzeUrl( } else -> RxHttp.get(url) .setOkClient(HttpHelper.getProxyClient(proxy)) - .addAll(fieldMap) + .addAllEncoded(fieldMap) .addAllHeader(headerMap) .toStrResponse().await() } @@ -324,7 +324,7 @@ class AnalyzeUrl( if (fieldMap.isNotEmpty()) { RxHttp.postForm(url) .setOkClient(HttpHelper.getProxyClient(proxy)) - .addAll(fieldMap) + .addAllEncoded(fieldMap) .addAllHeader(headerMap) .toByteArray().await() } else { @@ -337,7 +337,7 @@ class AnalyzeUrl( } else -> RxHttp.get(url) .setOkClient(HttpHelper.getProxyClient(proxy)) - .addAll(fieldMap) + .addAllEncoded(fieldMap) .toByteArray().await() } } From 695aae516667215854b28b471dddfbaaf026fcd2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 11:25:21 +0800 Subject: [PATCH 07/12] update log --- app/src/main/assets/updateLog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index bfbb28280..137bd48c1 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,10 @@ * 关注合作公众号 **[小说拾遗]** 获取好看的小说。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 +**2020/12/11** +* 修复bug +* 网络访问框架修改为RxHttp + **2020/12/11** * 修复因修改进度同步导致的bug From 97f1e590fbe4b70030708224c4e0f66efe51921d Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 11:47:00 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=91=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/book/explore/ExploreShowActivity.kt | 2 -- .../legado/app/ui/book/explore/ExploreShowViewModel.kt | 3 ++- .../io/legado/app/ui/widget/recycler/LoadMoreView.kt | 9 ++++++++- app/src/main/res/layout/activity_explore_show.xml | 5 +++-- app/src/main/res/layout/view_load_more.xml | 1 + 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt index 65e057b40..67a552833 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.book.explore import android.os.Bundle -import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -37,7 +36,6 @@ class ExploreShowActivity : VMBaseActivity>() + val errorLiveData = MutableLiveData() private var bookSource: BookSource? = null private val variableBook = SearchBook() private var exploreUrl: String? = null @@ -42,7 +43,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application page++ }.onError { it.printStackTrace() - toast(it.msg) + errorLiveData.postValue(it.msg) } } } diff --git a/app/src/main/java/io/legado/app/ui/widget/recycler/LoadMoreView.kt b/app/src/main/java/io/legado/app/ui/widget/recycler/LoadMoreView.kt index ac0e7659e..c8ade58c8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/recycler/LoadMoreView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/recycler/LoadMoreView.kt @@ -35,7 +35,7 @@ class LoadMoreView(context: Context, attrs: AttributeSet? = null) : FrameLayout( binding.tvText.invisible() binding.rotateLoading.show() } - + fun noMore(msg: String? = null) { hasMore = false binding.rotateLoading.hide() @@ -47,4 +47,11 @@ class LoadMoreView(context: Context, attrs: AttributeSet? = null) : FrameLayout( binding.tvText.visible() } + fun error(msg: String) { + hasMore = false + binding.rotateLoading.hide() + binding.tvText.text = msg + binding.tvText.visible() + } + } diff --git a/app/src/main/res/layout/activity_explore_show.xml b/app/src/main/res/layout/activity_explore_show.xml index 9bfab0f58..3de394692 100644 --- a/app/src/main/res/layout/activity_explore_show.xml +++ b/app/src/main/res/layout/activity_explore_show.xml @@ -25,8 +25,9 @@ - + android:layout_height="match_parent" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + tools:listitem="@layout/item_search" /> diff --git a/app/src/main/res/layout/view_load_more.xml b/app/src/main/res/layout/view_load_more.xml index c4a7ea04f..ed419aa0a 100644 --- a/app/src/main/res/layout/view_load_more.xml +++ b/app/src/main/res/layout/view_load_more.xml @@ -25,6 +25,7 @@ android:textSize="14sp" android:background="?attr/selectableItemBackground" android:visibility="invisible" + android:singleLine="true" tools:text="加载状态" tools:visibility="visible" /> From 5e7617da60a2f31ba049edb2978112a1f33d6c99 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 11:51:49 +0800 Subject: [PATCH 09/12] update log --- app/src/main/assets/updateLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 137bd48c1..bbb3c6ad3 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -5,7 +5,7 @@ **2020/12/11** * 修复bug -* 网络访问框架修改为RxHttp +* 网络访问框架修改为RxHttp, 有bug及时反馈 **2020/12/11** * 修复因修改进度同步导致的bug From 2d941d25b943e80b89372cedad19ad28e293528f Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 12:08:02 +0800 Subject: [PATCH 10/12] update lib --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 2403cd321..550d06afc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -166,7 +166,7 @@ dependencies { implementation 'org.jsoup:jsoup:1.13.1' //noinspection GradleDependency implementation 'cn.wanghaomiao:JsoupXpath:2.3.2' - implementation 'com.jayway.jsonpath:json-path:2.4.0' + implementation 'com.jayway.jsonpath:json-path:2.5.0' //JS rhino implementation 'com.github.gedoor:rhino-android:1.4' From 2e2919cee7eac7f8e5366d0d661a1b212a0ae582 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 12:40:47 +0800 Subject: [PATCH 11/12] update AnalyzeRule.kt --- .../java/io/legado/app/model/analyzeRule/AnalyzeRule.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index bf812256e..3954cee2f 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -163,7 +163,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { if (result is List<*>) { for (url in result as List<*>) { val absoluteURL = NetworkUtils.getAbsoluteURL(baseURL, url.toString()) - if (!absoluteURL.isNullOrEmpty() && !urlList.contains(absoluteURL)) { + if (absoluteURL.isNotEmpty() && !urlList.contains(absoluteURL)) { urlList.add(absoluteURL) } } @@ -231,7 +231,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { return if (str.isBlank()) { baseUrl ?: "" } else { - NetworkUtils.getAbsoluteURL(baseURL, str) ?: "" + NetworkUtils.getAbsoluteURL(baseURL, str) } } return str @@ -573,8 +573,8 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { rule = infoVal.toString() } //分离正则表达式 - val ruleStrS = rule.trim { it <= ' ' }.split("##") - rule = ruleStrS[0] + val ruleStrS = rule.split("##") + rule = ruleStrS[0].trim() if (ruleStrS.size > 1) { replaceRegex = ruleStrS[1] } From 8e307004ac9e99226a055e3c053a42117d48a8e2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 12 Dec 2020 12:57:31 +0800 Subject: [PATCH 12/12] update updateLog.md --- app/src/main/assets/updateLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index bbb3c6ad3..faccf6097 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,7 +3,7 @@ * 关注合作公众号 **[小说拾遗]** 获取好看的小说。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 -**2020/12/11** +**2020/12/12** * 修复bug * 网络访问框架修改为RxHttp, 有bug及时反馈