我想从我的Android应用中的Flickr照片中检索评论,并使用以下代码创建了JSONObjectRequest:
public void loadComments(ImageInfo photo) {
//clear the comments arraylist ready for new request
NetworkMgr.getInstance(this).commentsList.clear();
NetworkMgr netMgr = NetworkMgr.getInstance(getApplicationContext());
RequestQueue requestQueue = netMgr.requestQueue;
String id = photo.id;
String url = "https://api.flickr.com/services/rest/?method=flickr.photos.comments.getList&api_key=MY_API_KEY&format=json&nojsoncallback=1&extras=authorname,_content&photo_id=" + id;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, responseListener, errorListener);
requestQueue.add(request);
adapter.notifyDataSetChanged();
}
正确检索照片ID并将其附加到String url,下面我使用响应侦听器从每个评论对象中获取authorname和_content:
private Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject comments = response.getJSONObject("comments");
JSONArray commentList = comments.getJSONArray("comment");
for (int i = 0; i < commentList.length(); i++) {
ImageInfo newComment = new ImageInfo();
JSONObject comment = commentList.getJSONObject(i);
newComment.authorname = comment.getString("authorname");
newComment.commentContent = comment.getString("_content");
//add data to arraylist
NetworkMgr.getInstance(DetailsActivity.this).commentsList.add(newComment);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
adapter.notifyDataSetChanged();
}
};
private Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
但是,当我在浏览器中放置此URL以查看JSON数据时,注释JSONObject不存在。
How The JSON Response Should Look&lt;&lt;&lt;&lt; How My JSON Response Looks&lt;&lt;&lt;&lt;
有人知道为什么我无法检索JSON评论对象及其数据吗?
答案 0 :(得分:0)
将其放入loop
JSONObject allvaluesJsonObject=new JSONObject();
allvaluesJsonObject=jsonArray.getJSONObject(i);
删除此ImageInfo newComment = new ImageInfo();