0

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

21 Comments

@BenjaminSchwalb Crashes? how do you get the json by making a http request?
response = httpclient.execute(httpGet); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String json = reader.readLine();
Try this HttpResponse response = httpclient.execute(request); HttpEntity resEntity = response.getEntity(); String _response=EntityUtils.toString(resEntity)
If it crashes on the "first" row (mine I suppose), it means that response is null
JSONArray finalResult = new JSONArray(_response); With this i guess? Doesnt work either
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.