0

I have been trying to parse this keyless json array for a while. I know there are plenty of tutorials on how to parse json arrays, but they always follow the same pattern. I have this website over here that I am trying to parse and display the text. http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php

    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }

    JSONObject jObject = new JSONObject(result);

}

I am currently stuck here. What should I do from here?

Rami Yahia
  • 41
  • 3
  • 6

1 Answers1

1

This should not be so diffcicult. See below code. Look for syntax errors and handle exceptions, i just typed it.

JSONArray jsonArray = jObject.getJSONArray("notices");        
for(int i = 0; i < jsonArray.length(); i++) {
    String string = jsonArray.getString(i);
    Log.d("TAG", string); // do whatever you want with "string"
}
Varun
  • 33,833
  • 4
  • 49
  • 42
  • ohh thanks Varun I really appreciate it. it worked however, i don't get all the posts, i only get the most recent one which is New students, you have until 10am this morning to sign up for orientation on omnivox! It's going to be a really fun day with free food! – Rami Yahia Sep 04 '13 at 02:29
  • The for loop iterates over all the posts. see the log. you should see all the posts logged one after the other. You might want to add all the items from the for loop to a List or an Array. – Varun Sep 04 '13 at 05:26