js添加一个并发访问网络

pull/909/head
gedoor 4 years ago
parent 9078f91468
commit 3bf864fc23
  1. 23
      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.help.http.CookieStore
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.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.jsoup.Connection
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>
*/

Loading…
Cancel
Save