From 75fccf889e794defca1fa5a301eeb44b722250d4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 25 Apr 2020 18:42:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/AnalyzeUrl.kt | 50 +++++++++++++------ .../source/edit/BookSourceEditActivity.kt | 2 +- .../rss/source/edit/RssSourceEditActivity.kt | 2 +- 3 files changed, 36 insertions(+), 18 deletions(-) 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 ad96e781c..20f03797e 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 @@ -51,8 +51,8 @@ class AnalyzeUrl( private var queryStr: String? = null private val fieldMap = LinkedHashMap() private var charset: String? = null - private var bodyTxt: String? = null - private var body: RequestBody? = null + private var body: Any? = null + private var requestBody: RequestBody? = null private var method = RequestMethod.GET init { @@ -157,15 +157,25 @@ class AnalyzeUrl( baseUrl = it } if (urlArray.size > 1) { - val options = GSON.fromJsonObject>(urlArray[1]) + val options = GSON.fromJsonObject(urlArray[1]) options?.let { _ -> - options["method"]?.let { if (it.equals("POST", true)) method = RequestMethod.POST } - options["headers"]?.let { headers -> - GSON.fromJsonObject>(headers)?.let { headerMap.putAll(it) } + options.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST } + options.headers?.let { headers -> + if (headers is Map<*, *>) { + headerMap.putAll(headers as Map) + } + if (headers is String) { + GSON.fromJsonObject>(headers) + ?.let { headerMap.putAll(it) } + } + } + body = options.body + charset = options.charset + options.webView?.let { + if (it.toString().isNotEmpty()) { + useWebView = true + } } - options["body"]?.let { bodyTxt = it } - options["charset"]?.let { charset = it } - options["webView"]?.let { if (it.isNotEmpty()) useWebView = true } } } when (method) { @@ -179,11 +189,11 @@ class AnalyzeUrl( } } RequestMethod.POST -> { - bodyTxt?.let { - if (it.isJson()) { - body = RequestBody.create(jsonType, it) - } else { + body?.let { + if (it is String) { analyzeFields(it) + } else { + body = RequestBody.create(jsonType, GSON.toJson(it)) } } ?: let { body = FormBody.Builder().build() @@ -253,7 +263,7 @@ class AnalyzeUrl( } else { HttpHelper .getApiService(baseUrl, charset) - .postBody(url, body!!, headerMap) + .postBody(url, requestBody!!, headerMap) } } fieldMap.isEmpty() -> HttpHelper @@ -277,7 +287,7 @@ class AnalyzeUrl( params.requestMethod = method params.javaScript = jsStr params.sourceRegex = sourceRegex - params.postData = bodyTxt?.toByteArray() + params.postData = body?.toString()?.toByteArray() params.tag = tag return HttpHelper.ajax(params) } @@ -294,7 +304,7 @@ class AnalyzeUrl( } else { HttpHelper .getApiService(baseUrl, charset) - .postBodyAsync(url, body!!, headerMap) + .postBodyAsync(url, requestBody!!, headerMap) } } fieldMap.isEmpty() -> HttpHelper @@ -307,4 +317,12 @@ class AnalyzeUrl( return Res(NetworkUtils.getUrl(res), res.body()) } + data class UrlOption( + val method: String?, + val charset: String?, + val webView: Any?, + val headers: Any?, + val body: Any? + ) + } diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index f0e1feea1..c8887e770 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -373,7 +373,7 @@ class BookSourceEditActivity : "charset": "", "method": "POST", "body": "", - "headers": "{"User-Agent": ""}" + "headers": {"User-Agent": ""} } """.trimIndent() ) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index 8f90dfeed..ed18f8b69 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -206,7 +206,7 @@ class RssSourceEditActivity : "charset": "", "method": "POST", "body": "", - "headers": "{"User-Agent": ""}" + "headers": {"User-Agent": ""} } """.trimIndent()