求助:使用volley的JsonArrayRequest的时候报错

跑步_跑步 2015-05-28 02:59:02
org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray
com.android.volley.ParseError: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray
at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse(JsonArrayRequest.java:55)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:116)
Cause java.lang.String cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:91)
at org.json.JSONArray.<init>(JSONArray.java:103)
at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse(JsonArrayRequest.java:50)
... 1 more


不知道是什么原因,难道是服务器的json数据不对吗

我如果使用URLConnection去请求数据解析的时候,就没问题,能正常解析到?有没有谁碰到过类似的问题呢?

难道我要放弃使用volley了。。。
...全文
1882 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chen_TianLe 2016-08-06
  • 打赏
  • 举报
回复
我也遇到了这个问题,使用StringRequest返回xml数据,楼主是怎么解决的
xiaohuanqi 2016-07-12
  • 打赏
  • 举报
回复
你这个应该是服务端数据问题吧,你使用JsonArray返回的应该是一个Array类型的数据,你是不是返回的数据是string的,所以会出现解析错误
zhang106209 2016-07-12
  • 打赏
  • 举报
回复
楼上正解。。。。。。。
luyang7684 2016-07-12
  • 打赏
  • 举报
回复
服务器返回的是String类型,jsonArrayRequest的回调里传的是JsonArray类型,所以报错,让服务器返回JsonArray类型就对了
qq_25673053 2016-01-13
  • 打赏
  • 举报
回复
我也碰到了JSONArrayRequest请求出错的问题,求解 JsonArrayRequest arrayRequest = new JsonArrayRequest( HttpConstant.HQ_HOST + codes, new Listener<JSONArray>() { @Override public void onResponse(JSONArray arg0) { // TODO Auto-generated method stub for (int i = 0; i < arg0.length(); i++) { HQData data = new HQData(arg0.optJSONObject(i), hqentity.get(i).getBaoliu(), hqentity .get(i).getName()); listItemData.add(data); } handler.sendEmptyMessage(100); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub } }); queue.add(arrayRequest);
=10_01= 2016-01-13
  • 打赏
  • 举报
回复
感觉楼主应该要用的是 StringRequest。
tmxkdldw 2015-06-01
  • 打赏
  • 举报
回复
把分都给我吧。。。 import java.io.UnsupportedEncodingException; import org.json.JSONException; import org.json.JSONObject; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; import com.android.volley.toolbox.JsonObjectRequest; public class JsonObjectUTF8Request extends JsonObjectRequest{ public JsonObjectUTF8Request(String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener) { super(url, jsonRequest, listener, errorListener); // TODO Auto-generated constructor stub } @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { // solution 1: String jsonString = new String(response.data, "gbk"); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } } private void requestJson() { RequestQueue mQueue = Volley.newRequestQueue(getActivity()); JsonObjectUTF8Request jReq = new JsonObjectUTF8Request("http://1.5253.sinaapp.com/data.json",null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { JSONArray jy = null; try { new String(); jy = response.getJSONArray("data"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //等于零没有数据在处理 mPullRefreshListView.onRefreshComplete();//刷新完成 for (int i = 0; i < jy.length(); i++) { try { Log.d("sdfsdgfsdgfsd", response.toString()); result.add(convertWebPage(jy.getJSONObject(i))); } catch (JSONException e) { } } volleyListAdapter.setItemList(result); volleyListAdapter.notifyDataSetChanged(); mPullRefreshListView.onRefreshComplete();//刷新完成 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); //request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f)); jReq.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f)); mQueue.add(jReq); mQueue.start(); } private WebPage convertWebPage(JSONObject jsonObject) throws JSONException { String title = jsonObject.getString("title"); String description = jsonObject.getString("description"); String url = jsonObject.getString("url"); return new WebPage(title, description, url); }
  • 打赏
  • 举报
回复
大兄弟。。你可知道volley用post带参请求时为什么参数会丢失么? 还有,你这个可能是编码问题,看到乱码了 嘿嘿,错了别怪,我猜的,我也不太懂
sagittarius1988 2015-05-28
  • 打赏
  • 举报
回复
乱码了,你得设置下请求的编码
跑步_跑步 2015-05-28
  • 打赏
  • 举报
回复
引用 3 楼 jhjhy123 的回复:
你在logcat中打印的response.toString()的数据对吗
都没执行到这里呢,直接异常onErrorResponse在这里面打印了
SmallerRui 2015-05-28
  • 打赏
  • 举报
回复
你在logcat中打印的response.toString()的数据对吗
跑步_跑步 2015-05-28
  • 打赏
  • 举报
回复
引用 1 楼 qq_20646141 的回复:
贴代码 你请求的代码呢
代码应该没问题吧: JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { // TODO Auto-generated method stub Log.d(TAG, response.toString()); for(int i=0;i<response.length();i++){ try { JSONObject jsonObject = response.getJSONObject(i); Log.i(TAG, jsonObject.getString("PN") + ":"+jsonObject.getString("CD")+ ":"+jsonObject.getString("URL")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.e(TAG, error.getMessage(),error); } });
寒烟凝绿 2015-05-28
  • 打赏
  • 举报
回复
贴代码 你请求的代码呢

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧