Reputation: 1134
The JSON-String/Array I get as HTTP-Response is in a single line, like this:
[{
"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. I checked to content of the tokener, seems to be fine. Also checked to whole input (the String from http-response), it´s a valid JSON-string.
What might be worth mentioning is that the JSONObjects in the main-array also contain a sub-array.
Any Idea what might cause the crash? Or am I doing something completely wrong?
Upvotes: 0
Views: 661
Reputation: 157437
remove BufferedReader
, String json
and 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
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);
Upvotes: 1