I have a jsoup
function inside of a coroutine that scrapes information from a website and then gets a list of maps of all of the information it scraped. However, whenever I try to return the list, this is returned instead:
"Function0<java.util.List<java.util.Map<java.lang.String, ? extends java.lang.String>>>"
Here is the code for the 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 }
}
Here is the code for 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())
}
}
}
No matter where I put the return I can't get it to give me anything but an empty list. Yet when I log the list it gives me all of the information that was scraped.
I already tried this solution: How to return a value inside a coroutine scope kotlin, android and this solution: How to return value from coroutine scope
In the code, when you call return@withContext { table }
mean it will return () -> List
instead of the List
instance.
Just call return@withContext table
and it should be fixed.
Btw, I believe you should not set the function return type to Any
.
If you know exactly what you gonna do and you expect this function will return a List then the return type should be List<Map<String, String>>
, there is no reason to set it as Any
.
The immediate benefits that can be seen is if you set the return type as List<Map<String, String>>
and call return@withContext { table }
, the Android Studio IDE will show you what went wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.