Object cannot be converted to JSONArray 首先,最外层不是数组,是个object,不能array来解 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先JSONObject aJson = new JSONObject(urlPath);然后JSONArray array = aJson.optJSONArray("Result");最后把array放进你之前那个循环里 那个Result是我自己,忘记替换了,你的应该是news public static List<News> getListNews(String urlPath) throws Exception { List<News> mlists = new ArrayList<News>(); JSONObject aJson = new JSONObject(urlPath); JSONArray array = aJson.optJSONArray("news"); for (int i = 0; i < array.length(); i++) { JSONObject item = array.getJSONObject(i); String id = item.optString("id"); String title = item.optString("title"); String content = item.optString("content"); String outline = item.optString("outline"); mlists.add(new News(id, title, content, outline)); } return mlists; }试试 Quote: 引用 7 楼 u012374885 的回复: public static List<News> getListNews(String urlPath) throws Exception { List<News> mlists = new ArrayList<News>(); JSONObject aJson = new JSONObject(urlPath); JSONArray array = aJson.optJSONArray("news"); for (int i = 0; i < array.length(); i++) { JSONObject item = array.getJSONObject(i); String id = item.optString("id"); String title = item.optString("title"); String content = item.optString("content"); String outline = item.optString("outline"); mlists.add(new News(id, title, content, outline)); } return mlists; }又报异常 :org.json.JSONException:Value tttp of type java.lang.String cannot be converted to JSONObject Quote: 引用 7 楼 u012374885 的回复:org.json.JSONException:Value tttp of type java.lang.String cannot be converted to JSONObjectat xxxxxx getNewsList 你的urlpath是啥?readparse又是啥? public static byte[] readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[2048]; int len = 0; URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) { outStream.write(data, 0, len); } inStream.close(); return outStream.toByteArray(); }urlpath是我测试用的JSON网页 估计你打不开 我这里打开就是{"news":[{"content":"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一","id":1,"outline":"11111111111111111111111","title":"111111111"},{"content":"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二","id":2,"outline":"222222222222222222222222222222222222222","title":"222222222222"},{"content":"三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三","id":3,"outline":"333333333333333333333333333333333333333333333333333333333333333","title":"33333333333333"}]} 报空指针的代码public List<String> getData(){ try { news = JsonParse.getListNews(urlPath); } catch (Exception e) { e.printStackTrace(); } List<String> data = new ArrayList<String>(); for (int i = 1; i < news.size(); i++) { data.add("id:"+news.get(i).getId()); data.add("title:"+news.get(i).getTitle()); data.add("content:"+news.get(i).getContent()); data.add("outline:"+news.get(i).getOutline()); } return data; } 。urlpath不是json数据啊,我以为是json,你得先用你的readparse得到json啊,最后解的是new string(data)这个json数据 我做了TAG JSON数据已经能读出来了readParse(urlPath) 已经赋值给final byte[] data了final byte[] data = readParse(urlPath); JSONArray array = new JSONArray(new String(data))我也不知道哪出问题了 你看私信 能加QQ聊吗 public static List<News> getListNews(String urlPath) throws Exception { List<News> mlists = new ArrayList<News>(); byte[] data = readParse(urlPath); JSONObject aJson = new JSONObject(new String(data)); JSONArray array = aJson.optJSONArray("news"); for (int i = 0; i < array.length(); i++) { JSONObject item = array.optJSONObject(i); String id = item.optString("id"); String title = item.optString("title"); String content = item.optString("content"); String outline = item.optString("outline"); mlists.add(new News(id, title, content, outline)); } return mlists;}这样行不行 请教,用Eclipse调试Android程序时,总是启动新的AVD 播放rtsp视频一会儿后,不能播放了 android学习什么linux书合适? 如何使用adb keyevent解锁?求救 ListView Checkbox取值问题,贴代码,求前辈解决 请问下android中final的使用 用百度地图SDK将文字地址转化为地理经纬度坐标 求助大神,安卓虚拟机数据库找不到文件 Android调试Logcat不能正常显示信息 OPPO手机恢复出厂设置后又root了,有大神能找回通讯录吗 android怎样创建桌面快捷按钮 光标color与text color
JSONObject aJson = new JSONObject(urlPath);
然后
JSONArray array = aJson.optJSONArray("Result");
最后
把array放进你之前那个循环里
List<News> mlists = new ArrayList<News>();
JSONObject aJson = new JSONObject(urlPath);
JSONArray array = aJson.optJSONArray("news");
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String id = item.optString("id");
String title = item.optString("title");
String content = item.optString("content");
String outline = item.optString("outline");
mlists.add(new News(id, title, content, outline));
}
return mlists;
}试试
readparse又是啥?
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[2048];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len); }
inStream.close();
return outStream.toByteArray(); }
urlpath是我测试用的JSON网页 估计你打不开 我这里打开就是{"news":[{"content":"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一","id":1,"outline":"11111111111111111111111","title":"111111111"},{"content":"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二","id":2,"outline":"222222222222222222222222222222222222222","title":"222222222222"},{"content":"三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三","id":3,"outline":"333333333333333333333333333333333333333333333333333333333333333","title":"33333333333333"}]}
public List<String> getData(){
try {
news = JsonParse.getListNews(urlPath);
} catch (Exception e) {
e.printStackTrace();
}
List<String> data = new ArrayList<String>();
for (int i = 1; i < news.size(); i++) {
data.add("id:"+news.get(i).getId());
data.add("title:"+news.get(i).getTitle());
data.add("content:"+news.get(i).getContent());
data.add("outline:"+news.get(i).getOutline());
}
return data;
}
urlpath不是json数据啊,我以为是json,你得先用你的readparse得到json啊,最后解的是new string(data)这个json数据
readParse(urlPath) 已经赋值给final byte[] data了
final byte[] data = readParse(urlPath);
JSONArray array = new JSONArray(new String(data))
我也不知道哪出问题了 你看私信 能加QQ聊吗
List<News> mlists = new ArrayList<News>();
byte[] data = readParse(urlPath);
JSONObject aJson = new JSONObject(new String(data));
JSONArray array = aJson.optJSONArray("news");
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.optJSONObject(i);
String id = item.optString("id");
String title = item.optString("title");
String content = item.optString("content");
String outline = item.optString("outline");
mlists.add(new News(id, title, content, outline));
}
return mlists;
}
这样行不行