我想使用API中的信息,但出现了错误,我该怎么办?

huangapple go评论65阅读模式
英文:

I want use information from API and I got this error what should i do?

问题

我想从API显示信息但我遇到了这个错误

    Java.lang.Error: org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@1e465c6 of type java.lang.String cannot be converted to JSONObject
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
            at java.lang.Thread.run(Thread.java:923)
         Caused by: org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@1e465c6 of type java.lang.String cannot be converted to JSONObject

英文:

I want show information from API but I have this error:

Java.lang.Error: org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@1e465c6 of type java.lang.String cannot be converted to JSONObject
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@1e465c6 of type java.lang.String cannot be converted to JSONObject
package com.shayan.weatherapp

import android.nfc.Tag
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.ContactsContract.RawContacts
import android.util.Log
import android.widget.CheckBox
import okhttp3.*
import org.json.JSONObject
import java.io.IOException

class MainActivity : AppCompatActivity() {
    var checkbox:CheckBox? = null

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

        checkbox = findViewById<CheckBox>(R.id.checkBox)

        var client = OkHttpClient()

        var request = Request.Builder().url("https://jsonplaceholder.typicode.com/todos/1")
            .build()

        client.newCall(request).enqueue(object:Callback{
            override fun onFailure(call: Call, e: IOException) {

            }

            override fun onResponse(call: Call, response: Response) {
                val rawContact = response.body!!.toString()

                val jsonObject = JSONObject(rawContact)


                val myTodo = Todo(
                    jsonObject.getInt("userId"),
                    jsonObject.getInt("id"),
                    jsonObject.getString("title"),
                    jsonObject.getBoolean("completed"))

                runOnUiThread {
                    setValueForCheckBox(myTodo)
                }

            }

        })

    }

    fun setValueForCheckBox(todo: Todo){
        checkbox?.isChecked= todo.completed
        checkbox?.text = todo.title
    }
}```



</details>


# 答案1
**得分**: 0

你必须替换

```kotlin
val rawContact = response.body!!.toString()

val rawContact = response.body!!.string()

这是因为 toString() 方法返回的是 ResponseBody 对象的字符串表示,对于复杂对象,可能包含额外信息,如包名、内存地址等。相反,你只对响应主体的字符串表示感兴趣,所以你需要使用 string() 方法。

英文:

You have to replace

val rawContact = response.body!!.toString()

with

val rawContact = response.body!!.string()

This is because the toString() method returns a string representation of the ResponseBody object that for complex objects might contain additional information like package name, address in memory etc. Instead, you're insterested in a string representation of the body response: for that string() method is what you need.

huangapple
  • 本文由 发表于 2023年2月18日 18:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定