使用Android中的Volley从JSON解析数据

我尝试从“ https://api.instagram.com/v1/media/popular?client_id=”+ clientId解析JSON数据; 或者任何其他url,以多种不同的方式! 使用了几个JSONParsers,教程,读者……一切,但仍然无法从这些url获得任何东西。 现在我正在使用Volley库并且仍然无法使其工作,这是我的代码和您需要的一切,如果有人有任何想法,请分享它们。

public void LoadPictures() { mRequestQueue = Volley.newRequestQueue(this); mRequestQueue.add(new JsonObjectRequest(urlInst, null, new Listener() { @Override public void onResponse(JSONObject response) { try { parseJSON(response); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } })); 

这是我的parseJSON方法:

  private void parseJSON(JSONObject json) throws JSONException{ // JSONObject value = json.getJSONObject("value"); JSONArray items = json.getJSONArray("data"); for(int i=0;i<items.length();i++) { JSONObject c=(JSONObject) items.get(i); JSONObject user = c.getJSONObject("user"); String name= user.getString("username"); JSONObject img=c.getJSONObject("images"); JSONObject thum=img.getJSONObject("thumbnail"); String urlOfPic = thum.getString("url"); PhotoInst photoData=new PhotoInst (i, urlOfPic, name); photos.add(photoData); } 

这是我应该得到的JSON数据:

  "data": [{ "type": "image", "users_in_photo": [], "filter": "Gotham", "tags": [], "comments": { ... }, "caption": { "created_time": "1296656006", "text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)", "from": { "username": "cocomiin", "full_name": "", "type": "user", "id": "1127272" }, "id": "26329105" }, "likes": { "count": 35, "data": [{ "username": "mikeyk", "full_name": "Kevin S", "id": "4", "profile_picture": "..." }, {...subset of likers...}] }, "link": "http://instagr.am/p/BV5v_/", "user": { "username": "cocomiin", "full_name": "Cocomiin", "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg", "id": "1127272" }, "created_time": "1296655883", "images": { "low_resolution": { "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg", "width": 306, "height": 306 }, "thumbnail": { "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg", "width": 150, "height": 150 }, "standard_resolution": { "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg", "width": 612, "height": 612 } }, "id": "22518783", "location": null }, 

当我尝试使用随机Toasts来查看问题所在时,我可以看到我的方法中的onResponse根本没有调用LoadPictures? 我哪里失败了? 我只是在监督小事或其他事情吗?

@Mate – 从你的json可见,你得到一个JsonArray即“数据”。 因此,将Listener更改为Listener < JSONArray > whihc确保它返回JSONArray对象。 结果你的onResponse现在会变成,

 @Override public void onResponse(JSONArray response) { try { parseJSON(response); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

让我知道这个是否奏效。

您应首先validation的一些事项:

你确定你的清单中有以下内容吗?

  

Instagram需要某种API密钥/身份validation。 你确定你提供这个吗?

您的ErrorListener是否打印了堆栈跟踪? 如果是,你能提供吗?

这就是我要开始的地方。

这可以帮助你response = response.substring(5); 使得删除前5个字符,如data:并继续使用JSONArray jsonArray = new JSONArray(response);