使用OkHttp請求網絡,數據無法解析

一.日誌打印:

error:Value okhttp3.internal.http.RealResponseBody@49ebb98 of type java.lang.String cannot be converted to JSONArray

二.原文代碼:

1.錯誤:String str = response.body().toString();

2.正確:String str = response.body().string();

三.原因:

1.toString()可以將所有的的數據都轉換爲字符串,但是要排除null 和 undefined

.網頁代碼String()可以將null和undefined轉換爲字符串,但是沒法轉進制字符串

2.在stackoverflow上查到的解釋是:

toString(): This returns your object in string format.(以字符串的格式返回你的對象)

string(): This returns your response。(返回你的response)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章