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": "ravi@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "johnny_depp@gmail.com",
            "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.

4

2 回答 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")));
}

而且没有像文本这样的字符串 ,您正在阅读---> jo.getString("text");为什么?

于 2012-10-06T15:03:21.400 回答
0

使用 json api 真是太棒了。但是考虑对象映射器,例如 gson http://code.google.com/p/google-gson/或 jackson http://jackson.codehaus.org/。他们可能会使您的代码保持简洁。

于 2012-10-06T17:26:35.333 回答