OkHttp方法.toString()和.string()有什么区别? [英] What is the difference between the OkHttp methods .toString() and .string()?
问题描述
我有一段代码:
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
try {
Log.d("DEBUG POST=====>", response.body()!!.string())
}catch(e:IOException) {
e.printStackTrace()
}
}
使用response.body()!!.string()
时,我得到正确的输出和JSON正文.
When I use response.body()!!.string()
I get the correct output, and JSON body.
当我使用时:response.body().toString()
我得到okhttp3.ResponseBody$1@c626d25
有人可以告诉我这两种方法有什么区别吗?
Can anyone kindly tell me what is the difference between the two methods?
推荐答案
string()
不是有效的Kotlin(或Java)方法,因为两种语言都没有定义它.它由OkHttp在 ResponseBody
,这是获取类的实际字符串值的正确方法.它不会覆盖toString
,这意味着对toString()
的调用转到了Object
,这将以您得到的形式返回对象.确切地说,它返回对象的十六进制表示形式.
string()
isn't a valid Kotlin (or Java) method, as in neither of the languages define it. It's defined by OkHttp in ResponseBody
and it's the correct way to get the actual string value of the class. it doesn't override toString
, which means calls to toString()
go to Object
which returns the object in a form like you got. To be exact, it returns a hexadecimal representation of the object.
TL:DR; Java或Kotlin没有定义string()
方法,OkHttp库在ResponseBody
类中定义了方法. toString
不会被覆盖,从而使其返回类的十六进制表示形式而不是主体的字符串值.使用string()
而不是toString()
TL:DR; Java or Kotlin doesn't define a string()
method, the OkHttp library does in the ResponseBody
class. toString
isn't overridden, making it return the hexadecimal representation of the class instead of the string value of the body. Use string()
and not toString()
这篇关于OkHttp方法.toString()和.string()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!