How to parse JSON with Volley?

前端 未结 1 1678
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 18:31

I want to parse this json (my) with tutorial (source code).

My code is:

public class MainActivity extends Activity {
    // Log tag
    private stati         


        
相关标签:
1条回答
  • 2020-12-19 19:00

    Try this:

        String url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC";
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.getJSONObject(i);
                        // Do you fancy stuff
                        // Example: String gifUrl = jo.getString("url");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Anything you want
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    
    0 讨论(0)
提交回复
热议问题