Java 在 Android 中使用 Volley 解析来自 JSON 的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18699988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parsing data from JSON using Volley in Android
提问by Mate Kri?anac
I tried parsing JSON data from "https://api.instagram.com/v1/media/popular?client_id=" + clientId; or any other url, in a tons of different ways! Used couple of JSONParsers, tutorials, readers .. everything, but still can't to get anything from those urls. Now I am using Volley library and still can't get it to work, here is my code and everything you need, if anyone has any ideas , please share them.
我尝试从“ https://api.instagram.com/v1/media/popular?client_id=” + clientId解析 JSON 数据;或任何其他网址,以多种不同的方式!使用了几个 JSONParsers、教程、读者......一切,但仍然无法从这些 url 中获得任何信息。现在我正在使用 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();
}
}));
this is my parseJSON method:
这是我的 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);
}
this is JSON data I was supposed to get :
这是我应该得到的 JSON 数据:
"data": [{
"type": "image",
"users_in_photo": [],
"filter": "Gotham",
"tags": [],
"comments": { ... },
"caption": {
"created_time": "1296656006",
"text": "????a¥?¢??a?§??|????£?|????(^^)",
"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
},
when I try putting random Toasts to see where is the problem, I can see the onResponse in my method LoadPictures isn't called at all? Where am I failing ? am I just overseeing something small or something else?
当我尝试放置随机 Toast 以查看问题出在哪里时,我可以看到我的方法 LoadPictures 中的 onResponse 根本没有被调用?我哪里失败了?我只是在监督一些小事还是别的什么?
采纳答案by Atul O Holic
@Mate - As visible from your json, you are getting a JsonArray i.e. "data". Hence, change your Listener to Listener<JSONArray
> whihc ensures that it returns a JSONArray object. As a result your onResponse will now become,
@Mate - 从你的 json 中可以看出,你得到了一个 JsonArray,即“数据”。因此,将您的 Listener 更改为 Listener< JSONArray
> 以确保它返回一个 JSONArray 对象。因此,您的 onResponse 现在将变为,
@Override
public void onResponse(JSONArray response) {
try {
parseJSON(response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Let me know if this works.
让我知道这个是否奏效。
回答by Nic Raboy
A few things you should verify first:
您应该首先验证以下几点:
Are you sure you have the following in your manifest?
你确定你的清单中有以下内容吗?
<uses-permission android:name="android.permission.INTERNET" />
Instagram requires some sort of API Key / authentication. Are you sure you are providing this?
Instagram 需要某种 API 密钥/身份验证。你确定要提供这个吗?
Is your ErrorListener printing a stack trace? If it is, can you provide it?
您的 ErrorListener 是否打印堆栈跟踪?如果是,你能提供吗?
That is where I would start.
那就是我要开始的地方。
回答by Hemanth S Tobi
may this help you response = response.substring(5);
makes to remove first 5 characters like data:
and continue with JSONArray jsonArray = new JSONArray(response);
这可以帮助您 response = response.substring(5);
删除前 5 个字符,data:
然后继续JSONArray jsonArray = new JSONArray(response);