英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论