Android Kotlin API请求| gson处理后访问列表

问题描述 投票:1回答:1

我在api请求后尝试访问我在“ mainactivity”-> StartActivity中的列表:

“ mainactivity”-> StartActivity:


class StartActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.startseite)

        fetchJson()



        ...



    }
    fun fetchJson() {
        println("Attempting to Fetch JSON")

        val url = "https://....."
        val request = Request.Builder().url(url).build()
        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {

            override fun onResponse(call: Call, response: Response) {
                val body = response.body?.string()

                val gson = GsonBuilder().create()

                val statsVideoList: List<StatsVideo> = gson.fromJson(body, Array<StatsVideo>::class.java).toList()
                for (jetpack_featured_media_url in statsVideoList) {
                    println("The link is${jetpack_featured_media_url}")
                }

            }


            override fun onFailure(call: Call, e: IOException) {
                println("Failed to execute request")
            }
        })
    }
}

class StatsVideo (val id: Int, val status: String , val title: Title, val jetpack_featured_media_url: String, val link: String)

class Title (val rendered: String)

现在我想在此活动(StartActivity)中使用列表。例如。将jetpack_featured_media_url与毕加索一起加载到ImageView中。

我如何访问此字符串?

现在我要从component1中获取url /字符串jetpack_featured_media_url并使用毕加索加载它们。

我的代码:

Picasso.get().load(statsVideoList.component1().jetpack_featured_media_url).into(imageView)

但是在应用启动时,我收到此错误“

020-05-10 07:26:42.397 17384-17457/com.example.exampleE/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.example.example, PID: 17384
    java.lang.IllegalStateException: Method call should happen from the main thread.
        at com.squareup.picasso.Utils.checkMain(Utils.java:127)
        at com.squareup.picasso.RequestCreator.into(RequestCreator.java:679)
        at com.squareup.picasso.RequestCreator.into(RequestCreator.java:665)
        at vision.spectre.lernbox.StartActivity$fetchJson$1.onResponse(Startseite.kt:68)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
Method call should happen from the main thread.

但是如果我现在想创建一个按钮来启动一个新的活动并用它“转移”一个字符串

val buttonLatestVid = findViewById<ImageButton>(R.id.imageButton)
        buttonLatestVid.setOnClickListener {
            val intent = Intent(this, LatestVideoDetailActivity::class.java)
            intent.putExtra("VideoLink", statsVideoLost.component1().jetpack_featured_media_url)
            startActivity(intent)
        }

staatsVideoList是不可访问的,因为它不在onResponse中但是,如果我在onResponse中设置按钮,则意图会给我这个错误:

None of the following functions can be called with the arguments supplied: public constructor Intent(packageContext!, Context!, cls:Class<*>!) defined in android.content.Intentpublic constructor Intent(action: String!, uri: Uri!) defind in android.content.Intent
android api kotlin gson okhttp
1个回答
1
投票
RequestCreator.into()调用应从主线程进行。只需在执行任何UI更改之前更改线程即可。

如果使用协程:

Picasso.get().load(statsVideoList.component1().jetpack_featured_media_url).also { withContext(Dispatchers.Main.immediate) { it.into(imageView) } }

如果您不这样做:

Picasso.get().load(statsVideoList.component1().jetpack_featured_media_url).also { runOnUiThread { it.into(imageView) } }
© www.soinside.com 2019 - 2024. All rights reserved.