feat: 优化

pull/201/head
kunfei 5 years ago
parent 704e4f9cfa
commit 75fccf889e
  1. 50
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt

@ -51,8 +51,8 @@ class AnalyzeUrl(
private var queryStr: String? = null private var queryStr: String? = null
private val fieldMap = LinkedHashMap<String, String>() private val fieldMap = LinkedHashMap<String, String>()
private var charset: String? = null private var charset: String? = null
private var bodyTxt: String? = null private var body: Any? = null
private var body: RequestBody? = null private var requestBody: RequestBody? = null
private var method = RequestMethod.GET private var method = RequestMethod.GET
init { init {
@ -157,15 +157,25 @@ class AnalyzeUrl(
baseUrl = it baseUrl = it
} }
if (urlArray.size > 1) { if (urlArray.size > 1) {
val options = GSON.fromJsonObject<Map<String, String>>(urlArray[1]) val options = GSON.fromJsonObject<UrlOption>(urlArray[1])
options?.let { _ -> options?.let { _ ->
options["method"]?.let { if (it.equals("POST", true)) method = RequestMethod.POST } options.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST }
options["headers"]?.let { headers -> options.headers?.let { headers ->
GSON.fromJsonObject<Map<String, String>>(headers)?.let { headerMap.putAll(it) } if (headers is Map<*, *>) {
headerMap.putAll(headers as Map<out String, String>)
}
if (headers is String) {
GSON.fromJsonObject<Map<String, String>>(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) { when (method) {
@ -179,11 +189,11 @@ class AnalyzeUrl(
} }
} }
RequestMethod.POST -> { RequestMethod.POST -> {
bodyTxt?.let { body?.let {
if (it.isJson()) { if (it is String) {
body = RequestBody.create(jsonType, it)
} else {
analyzeFields(it) analyzeFields(it)
} else {
body = RequestBody.create(jsonType, GSON.toJson(it))
} }
} ?: let { } ?: let {
body = FormBody.Builder().build() body = FormBody.Builder().build()
@ -253,7 +263,7 @@ class AnalyzeUrl(
} else { } else {
HttpHelper HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset) .getApiService<HttpPostApi>(baseUrl, charset)
.postBody(url, body!!, headerMap) .postBody(url, requestBody!!, headerMap)
} }
} }
fieldMap.isEmpty() -> HttpHelper fieldMap.isEmpty() -> HttpHelper
@ -277,7 +287,7 @@ class AnalyzeUrl(
params.requestMethod = method params.requestMethod = method
params.javaScript = jsStr params.javaScript = jsStr
params.sourceRegex = sourceRegex params.sourceRegex = sourceRegex
params.postData = bodyTxt?.toByteArray() params.postData = body?.toString()?.toByteArray()
params.tag = tag params.tag = tag
return HttpHelper.ajax(params) return HttpHelper.ajax(params)
} }
@ -294,7 +304,7 @@ class AnalyzeUrl(
} else { } else {
HttpHelper HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset) .getApiService<HttpPostApi>(baseUrl, charset)
.postBodyAsync(url, body!!, headerMap) .postBodyAsync(url, requestBody!!, headerMap)
} }
} }
fieldMap.isEmpty() -> HttpHelper fieldMap.isEmpty() -> HttpHelper
@ -307,4 +317,12 @@ class AnalyzeUrl(
return Res(NetworkUtils.getUrl(res), res.body()) 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?
)
} }

@ -373,7 +373,7 @@ class BookSourceEditActivity :
"charset": "", "charset": "",
"method": "POST", "method": "POST",
"body": "", "body": "",
"headers": "{"User-Agent": ""}" "headers": {"User-Agent": ""}
} }
""".trimIndent() """.trimIndent()
) )

@ -206,7 +206,7 @@ class RssSourceEditActivity :
"charset": "", "charset": "",
"method": "POST", "method": "POST",
"body": "", "body": "",
"headers": "{"User-Agent": ""}" "headers": {"User-Agent": ""}
} }
""".trimIndent() """.trimIndent()

Loading…
Cancel
Save