错误
okhttp解析json数据报错
org.json.JSONException: Value com.squareup.okhttp.internal.http.RealResponseBody@3dcbbb8 of type java.lang.String cannot be converted to JSONArray
详细错误
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: org.json.JSONException: Value com.squareup.okhttp.internal.http.RealResponseBody@3dcbbb8 of type java.lang.String cannot be converted to JSONArray
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at org.json.JSONArray.<init>(JSONArray.java:98)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at org.json.JSONArray.<init>(JSONArray.java:110)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at com.example.electronicmall.test.User.UserRecordsActivity$1.onResponse(UserRecordsActivity.java:103)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
2022-09-20 11:09:58.619 13034-13282/com.example.electronicmall W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2022-09-20 11:09:58.620 13034-13282/com.example.electronicmall W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2022-09-20 11:09:58.620 13034-13282/com.example.electronicmall W/System.err: at java.lang.Thread.run(Thread.java:929)
解决方案
获取
o
k
h
t
t
p
okhttp
okhttp返回数据, 应该采用
R
e
s
p
o
n
s
e
B
o
d
y
ResponseBody
ResponseBody数据对象
.
s
t
r
i
n
g
(
)
.string()
.string()方法获取, 而不是
R
e
s
p
o
n
s
e
B
o
d
y
ResponseBody
ResponseBody数据对象
.
t
o
S
t
r
i
n
g
(
)
.toString()
.toString()方法获取
对于笔者而言 即将
ResponseBody data=response.body();
String json=data.toString();
processData(json);
改为
ResponseBody data=response.body();
String json=data.string();
processData(json);
原因
错误产生原因为
j
s
o
n
json
json解析出错
d
a
t
a
.
t
o
S
t
r
i
n
g
(
)
data.toString()
data.toString()返回为j
s
o
n
son
son对象
d
a
t
a
.
s
t
r
i
n
g
(
)
data.string()
data.string()返回的为
j
s
o
n
json
json数据。
d
a
t
a
.
t
o
S
t
r
i
n
g
data.toString
data.toString返回数据
d
a
t
a
.
s
t
r
i
n
g
(
)
data.string()
data.string()返回数据
参考文献
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈