casillas
casillas

Reputation: 16813

Fetch JSON Object via Android-Volley

I am using volley library for httprequest as follows. I am running the code in the debug code and putting break point to see json object but it neither returns json object nor show error messages.

private void productAll() {
   JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                Config.PRODUCT_URL,null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            Toast.makeText(MainActivity.this, "" + jsonObject.getString("imagePath"), Toast.LENGTH_SHORT).show();
                        }
                        catch(JSONException e) {
                            Toast.makeText(getActivity(), "Unable to parse data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(getActivity(), "Unable to fetch data: " + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                    }
        });
    }

Upvotes: 1

Views: 122

Answers (1)

Andrei Catinean
Andrei Catinean

Reputation: 883

Are you enqueueing the request?

Volley.newRequestQueue(this).add(request);

However, with Volley it's advisable to have one queue instance per app. You can consult the official documentation & tutorial here.

Upvotes: 1

Related Questions