3

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());
    }
Josh Boothe
  • 1,413
  • 4
  • 25
  • 40
  • what does result return and what is the expected result? – Raghunandan Jun 28 '13 at 15:02
  • It should return all of this http://pastebin.com/KLr2Czsb and it cuts it 1/4 of the way through http://pastebin.com/bgSqbNxi – Josh Boothe Jun 28 '13 at 15:05
  • i can't see what's wrong with the code. try logging result and check – Raghunandan Jun 28 '13 at 15:18
  • 1
    logcat isn't going to output absolutely everything. I have huge JSON strings also that get cut off, usually right around 1000 characters. – romo Jun 28 '13 at 15:18
  • @romo that could be a possibility, not all of the markers are showing on a map (will update my question with the for loop adding them, but everything is fine with them, as they display on the iOS app) – Josh Boothe Jun 28 '13 at 15:20
  • That's a single line, there aren't any newlines in your json. Maybe it's related to the size of the BufferedReader buffer? Can you try `JSONArray array = new JSONArray(HttpEntityUtils.toString(response.getEntity()))`? – VolatileDream Jun 28 '13 at 15:55

2 Answers2

1

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.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
Kalem
  • 1,132
  • 12
  • 33
0

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.

kau
  • 372
  • 1
  • 3
  • 14