From 26eff62778b05f9c229ae041348378af485e8c49 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 3 Nov 2019 11:01:41 +0800 Subject: [PATCH] up --- .../io/legado/app/help/http/AjaxWebView.kt | 70 ++++--------------- .../io/legado/app/help/http/HttpHelper.kt | 31 ++------ .../io/legado/app/help/http/RequestMethod.kt | 5 ++ .../app/model/analyzeRule/AnalyzeUrl.kt | 25 ++++--- 4 files changed, 38 insertions(+), 93 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/http/RequestMethod.kt 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 1ade0e294..abf3f0dae 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 @@ -1,7 +1,6 @@ package io.legado.app.help.http import android.annotation.SuppressLint -import android.content.Context import android.net.http.SslError import android.os.Build import android.os.Handler @@ -9,6 +8,7 @@ import android.os.Looper import android.os.Message import android.text.TextUtils import android.webkit.* +import io.legado.app.App import java.lang.ref.WeakReference import java.util.* @@ -49,7 +49,7 @@ class AjaxWebView { @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface") fun createAjaxWebView(params: AjaxParams, handler: Handler): WebView { - val webView = WebView(params.context.applicationContext) + val webView = WebView(App.INSTANCE) val settings = webView.settings settings.javaScriptEnabled = true settings.domStorageEnabled = true @@ -63,7 +63,7 @@ class AjaxWebView { } when (params.requestMethod) { RequestMethod.POST -> webView.postUrl(params.url, params.postData) - RequestMethod.GET, RequestMethod.DEFAULT -> webView.loadUrl( + RequestMethod.GET -> webView.loadUrl( params.url, params.headerMap ) @@ -75,29 +75,24 @@ class AjaxWebView { mWebView?.destroy() mWebView = null } - - - } - - fun ajax(params: AjaxParams) { - mHandler.obtainMessage(MSG_AJAX_START, params) - .sendToTarget() } - fun sniff(params: AjaxParams) { - mHandler.obtainMessage(MSG_SNIFF_START, params) - .sendToTarget() + fun load(params: AjaxParams) { + if (params.audioSuffix != "") { + mHandler.obtainMessage(MSG_SNIFF_START, params) + .sendToTarget() + } else { + mHandler.obtainMessage(MSG_AJAX_START, params) + .sendToTarget() + } } fun destroyWebView() { mHandler.obtainMessage(DESTROY_WEB_VIEW) } - class AjaxParams(val context: Context, private val tag: String) { - var requestMethod: RequestMethod? = null - get() { - return field ?: RequestMethod.DEFAULT - } + class AjaxParams(private val tag: String) { + var requestMethod = RequestMethod.GET var url: String? = null var postData: ByteArray? = null var headerMap: Map? = null @@ -112,41 +107,6 @@ class AjaxWebView { val isSniff: Boolean get() = !TextUtils.isEmpty(audioSuffix) - fun requestMethod(method: RequestMethod): AjaxParams { - this.requestMethod = method - return this - } - - fun url(url: String): AjaxParams { - this.url = url - return this - } - - fun postData(postData: ByteArray): AjaxParams { - this.postData = postData - return this - } - - fun headerMap(headerMap: Map): AjaxParams { - this.headerMap = headerMap - return this - } - - fun cookieStore(cookieStore: CookieStore): AjaxParams { - this.cookieStore = cookieStore - return this - } - - fun suffix(suffix: String): AjaxParams { - this.audioSuffix = suffix - return this - } - - fun javaScript(javaScript: String): AjaxParams { - this.javaScript = javaScript - return this - } - fun setCookie(url: String) { if (cookieStore != null) { val cookie = CookieManager.getInstance().getCookie(url) @@ -300,10 +260,6 @@ class AjaxWebView { const val DESTROY_WEB_VIEW = 4 } - enum class RequestMethod { - GET, POST, DEFAULT - } - interface CookieStore { fun setCookie(url: String, cookie: String) 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 44bb29346..c091a6c2c 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 @@ -77,43 +77,22 @@ object HttpHelper { @ExperimentalCoroutinesApi suspend fun ajax(params: AjaxWebView.AjaxParams): String = suspendCancellableCoroutine { block -> - val ajaxWebView = AjaxWebView() - ajaxWebView.callback = object : AjaxWebView.Callback() { + val webView = AjaxWebView() + webView.callback = object : AjaxWebView.Callback() { override fun onResult(result: String) { block.resume(result) { - ajaxWebView.destroyWebView() + webView.destroyWebView() } } override fun onError(error: Throwable) { block.resume(error.localizedMessage) { - ajaxWebView.destroyWebView() + webView.destroyWebView() } } } - ajaxWebView.ajax(params) - } - - @ExperimentalCoroutinesApi - suspend fun sniff(params: AjaxWebView.AjaxParams): String = - suspendCancellableCoroutine { block -> - val ajaxWebView = AjaxWebView() - ajaxWebView.callback = object : AjaxWebView.Callback() { - override fun onResult(result: String) { - block.resume(result) { - ajaxWebView.destroyWebView() - } - } - - override fun onError(error: Throwable) { - block.resume(error.localizedMessage) { - ajaxWebView.destroyWebView() - } - } - - } - ajaxWebView.sniff(params) + webView.load(params) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/RequestMethod.kt b/app/src/main/java/io/legado/app/help/http/RequestMethod.kt new file mode 100644 index 000000000..bba9f9761 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/RequestMethod.kt @@ -0,0 +1,5 @@ +package io.legado.app.help.http + +enum class RequestMethod { + GET, POST +} \ No newline at end of file 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 1673ceefc..ef4fa3e05 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 @@ -10,7 +10,9 @@ import io.legado.app.data.api.IHttpGetApi import io.legado.app.data.api.IHttpPostApi import io.legado.app.data.entities.BaseBook import io.legado.app.help.JsExtensions +import io.legado.app.help.http.AjaxWebView import io.legado.app.help.http.HttpHelper +import io.legado.app.help.http.RequestMethod import io.legado.app.utils.* import kotlinx.coroutines.Deferred import okhttp3.FormBody @@ -55,7 +57,7 @@ class AnalyzeUrl( private var charset: String? = null private var bodyTxt: String? = null private var body: RequestBody? = null - private var method = Method.GET + private var method = RequestMethod.GET private var webViewJs: String? = null val postData: ByteArray @@ -173,7 +175,7 @@ class AnalyzeUrl( if (urlArray.size > 1) { val options = GSON.fromJsonObject>(urlArray[1]) options?.let { - options["method"]?.let { if (it.equals("POST", true)) method = Method.POST } + options["method"]?.let { if (it.equals("POST", true)) method = RequestMethod.POST } options["headers"]?.let { headers -> GSON.fromJsonObject>(headers)?.let { headerMap.putAll(it) } } @@ -183,14 +185,14 @@ class AnalyzeUrl( } } when (method) { - Method.GET -> { + RequestMethod.GET -> { urlArray = url.split("?") url = urlArray[0] if (urlArray.size > 1) { analyzeFields(urlArray[1]) } } - Method.POST -> { + RequestMethod.POST -> { bodyTxt?.let { if (it.isJson()) { body = it.toRequestBody(jsonType) @@ -250,14 +252,10 @@ class AnalyzeUrl( return SCRIPT_ENGINE.eval(jsStr, bindings) } - enum class Method { - GET, POST - } - @Throws(Exception::class) fun getResponse(): Call { return when { - method == Method.POST -> { + method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { HttpHelper .getApiService(baseUrl) @@ -280,7 +278,7 @@ class AnalyzeUrl( @Throws(Exception::class) fun getResponseAsync(): Deferred> { return when { - method == Method.POST -> { + method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { HttpHelper .getApiService(baseUrl) @@ -299,4 +297,11 @@ class AnalyzeUrl( .getMapAsync(url, fieldMap, headerMap) } } + + suspend fun getResultByWebView(tag: String): String { + val params = AjaxWebView.AjaxParams(tag) + params.url = url + params.requestMethod = method + return HttpHelper.ajax(params) + } }