我作为 HTTP-Response 得到的 JSON-String/Array 在一行中,如下所示:
[{
"haendlerName": "Zielpunkt",
"shops": [{
"shopId": 243779,
"ort": "Wien",
"strasse": "Erdbergstraße 61",
"plz": "1030",
"lat": 48.19867,
"lon": 16.400263,
"distance": 0.14937061106081023,
"openinghours": null
}],
"imageLink": "http://images.schnapp.at/images/zielpunkt__e349e2a937b5bf4f78e0fb3063b1fca8.png",
"account_id": 171619
}, ...
我是这样加载的:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
HttpEntity resEntity = response.getEntity();
String response2=EntityUtils.toString(resEntity);
JSONArray finalResult = new JSONArray(response2);
编辑:工作版本!
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
String json = EntityUtils.toString(response.getEntity());
finalResult = new JSONArray(json);
但是在最后一行它崩溃了,我得到的唯一错误是 NullPointerException。我检查了tokener的内容,似乎没问题。还检查了整个输入(来自 http-response 的字符串),它是一个有效的 JSON 字符串。
值得一提的是主数组中的 JSONObjects 还包含一个子数组。
任何想法可能导致崩溃?还是我做错了什么?