[英]Loading a JSON-Array fails
The JSON-String/Array I get as HTTP-Response is in a single line, like this: 我通过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
}, ...
I´m loading it like this: 我正在像这样加载它:
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);
EDIT: WORKING VERSION! 编辑:工作版本!
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
String json = EntityUtils.toString(response.getEntity());
finalResult = new JSONArray(json);
But in the very last line it crashes, only error I got is a NullPointerException. 但是在最后一行它崩溃了,我得到的唯一错误是NullPointerException。 I checked to content of the tokener, seems to be fine.
我检查了令牌程序的内容,似乎还可以。 Also checked to whole input (the String from http-response), it´sa valid JSON-string.
还检查了整个输入(http响应中的字符串),它是有效的JSON字符串。
What might be worth mentioning is that the JSONObjects in the main-array also contain a sub-array. 可能值得一提的是,主数组中的JSONObjects也包含一个子数组。
Any Idea what might cause the crash? 任何想法可能导致崩溃的原因吗? Or am I doing something completely wrong?
还是我做错了什么?
remove BufferedReader
, String json
and JSONTokener tokener
. 删除
BufferedReader
, String json
和JSONTokener tokener
。 the try this way 尝试这种方式
String json = EntityUtils.toString(response.getEntity());
JSONArray finalResult = new JSONArray(json);
EntityUtils.toString
reads the content of the Entity
and returns it as String
EntityUtils.toString
读取Entity
的内容并将其作为String
返回
you should replace: 您应该更换:
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);
with 同
String json = EntityUtils.toString(response.getEntity());
JSONArray finalResult = new JSONArray(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.