0

I manage to retrieve Json content from the URL: http://twitter.com/statuses/public_timeline.json Which provide just tweets. Now I want to retrieve another JSON array from another URL, which has the follwoing format:

  {
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "[email protected]",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "[email protected]",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",

    },
    .
    .
}

The diference as you can verify is that the one JSON array has a name and the second not. The code that I was used for the tweet retrieving, was the following one:

  private class LoadListTask extends AsyncTask<Void, Tweet, Void> {

    private final ProgressDialog pd = new ProgressDialog(TweetTestActivity.this);

      // can use UI thread here
      @SuppressWarnings("unchecked")
    protected void onPreExecute() {
          ((ArrayAdapter<String>)getListAdapter()).clear();
         this.pd.setMessage("Loading List...");
         this.pd.show();
      }

    protected Void doInBackground(Void... ars) {
         // TODO    4 new activity with custom adapter to show schedules
         try {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet("http://twitter.com/statuses/public_timeline.json");

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            JSONArray ja = new JSONArray(client.execute(get, responseHandler));

            for (int i = 0; i < ja.length(); i++) {
                 JSONObject jo = (JSONObject) ja.get(i);
                 publishProgress(new Tweet(jo.getString("id_str"),jo.getString("text")));
            }
         } catch (IOException e) {
             e.printStackTrace();
         } catch (JSONException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
        }

         return null;
     }

     @SuppressWarnings("unchecked")
    protected void onProgressUpdate(Tweet... progress) {
         ((ArrayAdapter<Tweet>)getListAdapter()).add(progress[0]);
         }

     protected void onPostExecute(Void result) {
         pd.dismiss();
     }
 }

Question: What changes should i perform to retrieve the contacts array? Because when i just change the id_str to gender(and the URL of course) i take an error which says: org.json.JSONException: No value for gender.

2 Answers 2

1
JSONObject jObj = new JSONObject (client.execute(get, responseHandler));

JSONArray ja = new JSONArray(jObj.getJSONArray("contacts"));
enter code here
for (int i = 0; i < ja.length(); i++) {
    JSONObject jo = (JSONObject) ja.get(i);
    publishProgress(new Tweet(jo.getString("id_str"),jo.getString("text")));
}

And there is no String like text and you are reading ---> jo.getString("text"); why ?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you MAC for your answer. The jo.getString("text"); was used in order to retrieve only the text attribute of the JSON object
MAC i did the changes you proposed but in the JSONArray line it hit's me an error. It says:'The constructor JSONArray(JSONArray) is undefined'. The solutuion that is propose by the IDE is to cast argument1 to Collection. I did this change, but it doesn't print the results.
0

That's awesome to use json api. But consider object mapper's like gson http://code.google.com/p/google-gson/ or jackson http://jackson.codehaus.org/ . They may keep your code concise.

Comments

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.