parsing data from JSON using Volley in Android
@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,
@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.
Mate Križanac
Updated on June 04, 2022Comments
-
Mate Križanac almost 2 years
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.
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:
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 :
"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 },
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?
-
Mate Križanac over 10 yearsyes to all above except error listener, it's not even triggered.