![](/img/trans.png)
[英]How to cancel (return from) a Kotlin coroutine from within the scope immediately?
[英]How to return a variable from inside coroutine scope in Kotlin
我在協程中有一個jsoup
function,它從網站上抓取信息,然后獲取它抓取的所有信息的地圖列表。 但是,每當我嘗試返回列表時,都會返回:
"Function0<java.util.List<java.util.Map<java.lang.String, ? extends java.lang.String>>>"
這是 function 的代碼:
suspend fun popularHome(
pg: String
): Any = withContext(Dispatchers.IO) {
val table = mutableListOf<Map<String, String>>()
val doc: Document = Jsoup
.connect("https://www.novelupdates.com/novelslisting/?sort=2&order=1&status=1&pg=$pg")
.userAgent("Mozilla/5.0")
.referrer("http://www.google.com")
.ignoreContentType(true)
.get()
val imageLists = doc.getElementsByClass("search_img_nu")
val lists = doc.getElementsByClass("search_main_box_nu")
for ((list, imageList) in lists.zip(imageLists)) {
val searches: Elements = list.getElementsByClass("search_title")
val imageSearches: Elements = imageList.getElementsByTag("img")
for ((search, imageSearch) in searches.zip(imageSearches)) {
val titles = search.getElementsByTag("a")
val ranks = search.getElementsByClass("genre_rank")
for ((title, rank) in titles.zip(ranks)) {
val link: String = title.attr("href") // Book Link
val titleName: String = title.text() // Book Title
val imageLink: String = imageSearch.attr("src") // Book Image Link
val rankName: String =
rank.text().replace("#", "") // Book Popularity Rank
table.add(
mutableMapOf<String, String>(
"rank" to rankName,
"title" to titleName,
"link" to link,
"image" to imageLink
)
)
}
}
}
Log.i("tag", table.toString())
return@withContext { table }
}
這是Main Activity
的代碼:
package com.example.sushi
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import com.example.sushi.service.repos.novelupdates.com.NovelUpdatesScraper
import com.example.sushi.ui.styling.SushiTheme
import kotlinx.coroutines.*
class MainActivity : ComponentActivity() {
val scrape = NovelUpdatesScraper()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SushiTheme {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Hey")
}
}
}
lifecycleScope.launch {
Log.i("tag", scrape.popularHome("1").toString())
}
}
}
無論我把退貨放在哪里,除了一個空列表,我什么也得不到。 然而,當我記錄列表時,它會為我提供所有被抓取的信息。
我已經嘗試過這個解決方案: How to return a value inside a coroutine scope kotlin, android這個解決方案: How to return value from coroutine scope
在代碼中,當您調用return@withContext { table }
時,意味着它將返回() -> List
而不是List
實例。
只需調用return@withContext table
,它就應該被修復。
順便說一句,我相信你不應該將 function 返回類型設置為Any
。
如果你確切地知道你要做什么並且你期望這個 function 將返回一個 List 那么返回類型應該是List<Map<String, String>>
,沒有理由將它設置為Any
。
可以看到的直接好處是,如果您將返回類型設置為List<Map<String, String>>
並調用return@withContext { table }
,Android Studio IDE 將告訴您哪里出了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.