I’m currently trying to put a twitter feed into my app and currently everything works except when I try to get the image url field from the JSON returned.
Here is my code to parse the JSON:
public ArrayList<Tweet> getTweets() {
String searchUrl =
"http://twitter.com/statuses/user_timeline/vogella.json";
ArrayList<Tweet> tweets =
new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
Log.e("", "responseBody = " + responseBody);
JSONArray arr = null;
try {
arr = new JSONArray(responseBody);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < arr.length(); i++) {
try {
jsonObject = arr.getJSONObject(i);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Tweet tweet = null;
try {
tweet = new Tweet(
jsonObject.getString("profile_image_url"),
jsonObject.getString("text"),
jsonObject.getString("created_at")
);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tweets.add(tweet);
}
return tweets;
}
And here is the error I get:
02-14 00:19:18.672: W/System.err(809): org.json.JSONException:JSONObject["profile_image_url"] not found.
Despite the “profile_image_url” being present – click the link to see the JSON – LINK. Everything else in the feed appears to be retrievable so why cant I get the image url?
Your
jsonObjectvariable refers to the top level array element of your response, which contains elements likein_reply_to_status_id,geo, etc. Theprofile_image_urlproperty is not a property in that top level array element, but rather a child element of theuserproperty.To access the
profile_background_image_url, you would have to do something roughly like the following: