Josh Boothe
Josh Boothe

Reputation: 1423

JSON response cutting off part way through

When downloading a JSON array, it cuts off 1/4 of the way through the string, its pretty huge - but it should get the entire string.

There are no errors thrown in the LogCat. This is the method I am using, I have been through it a few times and cant see a reason why it is cutting off. I am pretty new to this however.

public static JSONArray getJSONfromURL(String url){

    //initialize
    InputStream is = null;   
    String result = "";   
    JSONArray jArray = null;

    //http post
    try {

        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(url);    
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        is = entity.getContent();    

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());   
    }
    //convert response to string

    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);  
        StringBuilder sb = new StringBuilder();   
        String line = null;

        while ((line = reader.readLine()) != null) {   
            sb.append(line + "\n");
        }

        is.close();
        result=sb.toString();

    } catch (Exception e) {    
        Log.e("log_tag", "Error converting result "+e.toString());
    }
    //try parse the string to a JSON object

    try {
        Log.d("log_tag", "jresult: " + result + "finish");
        jArray = new JSONArray(result);

        //Log.e("log_tag", "result: " + jArray.toString());

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
}

I think once this is cracked I will be set. I will make this a class to use in future projects too so I dont have to keep rebuilding it!

EDIT: For loop where markers should be added to map:

try{
        for(int i=0;i<arrayResultFromURL.length();i++){
            JSONObject json_data = arrayResultFromURL.getJSONObject(i);

            // assign attributes from the JSON string to local variables
            String id =json_data.getString("id");
            String title =json_data.getString("title");
            String strap =json_data.getString("strap");
            Double lat = (double) json_data.getDouble("lat");
            Double lon = (double) json_data.getDouble("long");

            theMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lon))
            .title(title)
            .snippet(strap));
        }
    }catch (Exception e){
        Log.e("log_tag", "error in array: " + e.toString());
    }

Upvotes: 3

Views: 7424

Answers (2)

Kalem
Kalem

Reputation: 1132

Maybe your problem comes from the way your are treating the response object. Check this thread.

If not try to check the size of the response first to see if you are receving all.

httpResponse.getEntity().getContentLength()

Also just in case you didn't know there is a nice library (i've been using it since i found it) that simplifies json parsing ckeck it out here.

Upvotes: 1

kau
kau

Reputation: 372

These type of things can best be done by libraries such as GSON or Jackson

Also, if your goal is to create a JSONArray, there is a constructor that takes in a JSONTokener. JSONTokener can in turn be constructed from your InputStream.

Upvotes: 0

Related Questions