java - 在 Android 中使用 Volley 解析来自 JSON 的数据

标签 java android json android-volley

我尝试从“https://api.instagram.com/v1/media/popular?client_id=”解析 JSON 数据 + 客户编号;或任何其他网址,以多种不同的方式!使用了几个 JSONParsers、教程、阅读器……一切,但仍然无法从这些网址中获取任何内容。现在我正在使用 Volley 库,但仍然无法正常工作,这是我的代码和您需要的一切,如果有人有任何想法,请分享。

public  void LoadPictures()   {
    mRequestQueue =  Volley.newRequestQueue(this);
    mRequestQueue.add(new JsonObjectRequest(urlInst, null,
            new Listener<JSONObject>() {
                @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
},

当我尝试放置随机 Toast 以查看问题出在哪里时,我可以看到我的方法 LoadPictures 中的 onResponse 根本没有被调用?我哪里失败了?我只是在监督一些小事还是其他事?

最佳答案

@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();
    }
}

让我知道这是否有效。

关于java - 在 Android 中使用 Volley 解析来自 JSON 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699988/

相关文章:

java - 以编程方式创建的布局底部的按钮

javascript - 迭代响应数据以创建谷歌地图对象

java - 如何实现 dao 来获取列表而不使用 createCriteria() 并获取具有以下属性的列表?

java - NoSuchMethodError 错误创建 session 工厂

java - 在 Android 应用程序中,您将如何返回离开页面时的状态?

php - MySQL Convert Json 更改列名

javascript - 在 html 中使用上传的 csv 文件

Java 垃圾收集器 :old generation becomes larger and larger and cannot be reclaimed

java - 如何创建包含两个元素的嵌套 xml 结构?

android - 无法在 eclipse 中导入 android 支持库