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 正文。
当我使用:response.body().toString()
我得到 okhttp3.ResponseBody@c626d25
谁能告诉我这两种方法有什么区别?
string()
不是有效的 Kotlin(或 Java)方法,因为两种语言都没有定义它。它由 OkHttp 在 ResponseBody
中定义,它是获取 class 的实际字符串值的正确方法。它不会覆盖 toString
,这意味着对 toString()
的调用会转到 Object
,其中 return 是您所获得的表单中的对象。确切地说,它 return 是对象的十六进制表示形式。
TL:DR; Java 或者 Kotlin 没有定义 string()
方法,OkHttp 库在 ResponseBody
class。 toString
未被覆盖,使其 return 成为 class 的十六进制表示,而不是正文的字符串值。使用 string()
而不是 toString()
根据documentation for OkHttp's ResponseBody,string()
函数:
Returns the response as a string decoded with the charset of the Content-Type header. If that header is either absent or lacks a charset, this will attempt to decode the response body in accordance to its BOM or UTF-8. Closes ResponseBody automatically.
相比之下,所有 Java/Kotlin 对象上的 toString()
方法在本例中并未为 ResponseBody
定义。在这种情况下,将调用 java.lang.Object
上的版本,并且标准实现是发出对象的 class 名称和对象的哈希码(十六进制)。
来自JavaDoc for Object.toString()
:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
简而言之,这些方法有意做不同的事情。
我有一段代码:
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 正文。
当我使用:response.body().toString()
我得到 okhttp3.ResponseBody@c626d25
谁能告诉我这两种方法有什么区别?
string()
不是有效的 Kotlin(或 Java)方法,因为两种语言都没有定义它。它由 OkHttp 在 ResponseBody
中定义,它是获取 class 的实际字符串值的正确方法。它不会覆盖 toString
,这意味着对 toString()
的调用会转到 Object
,其中 return 是您所获得的表单中的对象。确切地说,它 return 是对象的十六进制表示形式。
TL:DR; Java 或者 Kotlin 没有定义 string()
方法,OkHttp 库在 ResponseBody
class。 toString
未被覆盖,使其 return 成为 class 的十六进制表示,而不是正文的字符串值。使用 string()
而不是 toString()
根据documentation for OkHttp's ResponseBody,string()
函数:
Returns the response as a string decoded with the charset of the Content-Type header. If that header is either absent or lacks a charset, this will attempt to decode the response body in accordance to its BOM or UTF-8. Closes ResponseBody automatically.
相比之下,所有 Java/Kotlin 对象上的 toString()
方法在本例中并未为 ResponseBody
定义。在这种情况下,将调用 java.lang.Object
上的版本,并且标准实现是发出对象的 class 名称和对象的哈希码(十六进制)。
来自JavaDoc for Object.toString()
:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
简而言之,这些方法有意做不同的事情。