我试图从Instagram照片中获取评论。它有效,除了它只打印前8个注释。我的代码基于http://www.ibm.com/developerworks/xml/library/x-instagram1/index.html,因此从理论上说它应该可以工作(那里是用PHP完成的)。在我的某个地方可能有一个愚蠢的错误,所以如果有人能指出它我会很感激它的串联<3。
// Construct url to get Instagram feed
mAccessToken = sharedPref.getString(API_ACCESS_TOKEN, null);
// todo: we have to switch to -1, right?
String feed = "https://api.instagram.com/v1/users/self/feed?access_token="+mAccessToken;
// Making a request to url and getting response
String json = sh.makeServiceCall(feed, ServiceHandler.GET);
// do the stuff
if (json != null) {
try {
// make a new json object
JSONObject obj = new JSONObject(json);
// data
data = obj.getJSONArray(TAG_DATA);
// looping through the data
for (int i=0; i < data.length(); i++) {
JSONObject c = (JSONObject)data.get(i);
JSONObject img = c.getJSONObject(TAG_IMAGES);
JSONObject thum = img.getJSONObject(TAG_LOW_RESOLUTION);
JSONObject comments = c.getJSONObject(TAG_COMMENTS);
JSONObject user = c.getJSONObject(TAG_USER);
JSONObject likes = c.getJSONObject(TAG_LIKES);
String likes_count = likes.getString(TAG_LIKES_COUNT);
String profile_picture = user.getString(TAG_PROFILE_PICTURE);
String username = user.getString(TAG_USERNAME);
String comments_count = comments.getString(TAG_COMMENTS_COUNT);
System.out.println(comments_count);
String urlOfPic = thum.getString(TAG_URL);
JSONArray comments_data = comments.getJSONArray("data");
for (int z=0; z < comments_data.length(); z++) {
String commentText = ((JSONObject) comments_data.get(z)).getString("text").toString();
System.out.println("commentText ==" +commentText);
long created_time = Long.parseLong((((JSONObject) comments_data.get(z)).getString("created_time")).toString());
Date date = new Date(created_time * 1000);
System.out.println(date);
}
System.out.println("-------------------------------------------------------------");
HashMap<String, String> photos = new HashMap<String, String>();
photos.put("likes.count", likes_count);
photos.put(TAG_PROFILE_PICTURE, profile_picture);
photos.put(TAG_USERNAME, username);
photos.put(TAG_URL, urlOfPic);
photos.put(TAG_COMMENTS_COUNT, comments_count);
instagram_list.add(photos);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}