parsing data from JSON using Volley in Android

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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 :

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?

回答1:

@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.



回答2:

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?

Is your ErrorListener printing a stack trace? If it is, can you provide it?

That is where I would start.



回答3:

may this help you response = response.substring(5); makes to remove first 5 characters like data: and continue with JSONArray jsonArray = new JSONArray(response);



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!