org.json.JSONException: Value com.squareup.okhttp.internal.http.RealResponseBody@3dcbbb8 of type jav

错误

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()返回数据请添加图片描述

参考文献

org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@da68fa1 of type java.lang.String cannot be converted to JSONObject

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞滕人生TYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值