js添加一个并发访问网络

pull/909/head
gedoor 4 years ago
parent 9078f91468
commit 3bf864fc23
  1. 59
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -6,10 +6,13 @@ import androidx.annotation.Keep
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.CookieStore
import io.legado.app.help.http.SSLHelper import io.legado.app.help.http.SSLHelper
import io.legado.app.help.http.StrResponse
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.jsoup.Connection import org.jsoup.Connection
import org.jsoup.Jsoup import org.jsoup.Jsoup
@ -40,6 +43,26 @@ interface JsExtensions {
} }
} }
/**
* 并发访问网络
*/
@ExperimentalCoroutinesApi
fun fetchAll(urlList: List<String>): Array<StrResponse?> {
return runBlocking {
val asyncArray = Array(urlList.size) {
async {
val url = urlList[it]
val analyzeUrl = AnalyzeUrl(url)
analyzeUrl.getStrResponse(url)
}
}
val resArray = Array<StrResponse?>(urlList.size) {
asyncArray[it].await()
}
resArray
}
}
/** /**
* 访问网络,返回Response<String> * 访问网络,返回Response<String>
*/ */
@ -62,8 +85,8 @@ interface JsExtensions {
fun downloadFile(content: String, url: String): String { fun downloadFile(content: String, url: String): String {
val type = AnalyzeUrl(url).type ?: return "" val type = AnalyzeUrl(url).type ?: return ""
val zipPath = FileUtils.getPath( val zipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}" "${MD5Utils.md5Encode16(url)}.${type}"
) )
FileUtils.deleteFile(zipPath) FileUtils.deleteFile(zipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
@ -81,8 +104,8 @@ interface JsExtensions {
fun unzipFile(zipPath: String): String { fun unzipFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" if (zipPath.isEmpty()) return ""
val unzipPath = FileUtils.getPath( val unzipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
FileUtils.getNameExcludeExtension(zipPath) FileUtils.getNameExcludeExtension(zipPath)
) )
FileUtils.deleteFile(unzipPath) FileUtils.deleteFile(unzipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
@ -104,7 +127,7 @@ interface JsExtensions {
for (f in it) { for (f in it) {
val charsetName = EncodingDetect.getEncode(f) val charsetName = EncodingDetect.getEncode(f)
contents.append(String(f.readBytes(), charset(charsetName))) contents.append(String(f.readBytes(), charset(charsetName)))
.append("\n") .append("\n")
} }
contents.deleteCharAt(contents.length - 1) contents.deleteCharAt(contents.length - 1)
} }
@ -118,12 +141,12 @@ interface JsExtensions {
*/ */
fun get(urlStr: String, headers: Map<String, String>): Connection.Response { fun get(urlStr: String, headers: Map<String, String>): Connection.Response {
return Jsoup.connect(urlStr) return Jsoup.connect(urlStr)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory)
.ignoreContentType(true) .ignoreContentType(true)
.followRedirects(false) .followRedirects(false)
.headers(headers) .headers(headers)
.method(Connection.Method.GET) .method(Connection.Method.GET)
.execute() .execute()
} }
/** /**
@ -131,13 +154,13 @@ interface JsExtensions {
*/ */
fun post(urlStr: String, body: String, headers: Map<String, String>): Connection.Response { fun post(urlStr: String, body: String, headers: Map<String, String>): Connection.Response {
return Jsoup.connect(urlStr) return Jsoup.connect(urlStr)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory)
.ignoreContentType(true) .ignoreContentType(true)
.followRedirects(false) .followRedirects(false)
.requestBody(body) .requestBody(body)
.headers(headers) .headers(headers)
.method(Connection.Method.POST) .method(Connection.Method.POST)
.execute() .execute()
} }
/** /**

Loading…
Cancel
Save