Mulgard
Mulgard

Reputation: 10589

How to return a JSONObject using Volley

I wrote this method:

public JSONObject getJSONFromUrl(String url, final Map<String, String> params) {
    StringRequest post = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        /**
         *
         * @param response
         */
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);

                Log.v("JSONParser", "response: " + jsonResponse.toString());
            } catch(JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        /**
         *
         * @param error
         */
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }) {
        /**
         *
         * @return
         * @throws AuthFailureError
         */
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return params;
        }
    };

    Volley.newRequestQueue(this.context).add(post);

    return null;
}

I need to return that JSONObject that i create in onResponse but i cant figure out how to do it. Is there any way to return the JSONObject? I also cant assign the JSONObject to a final variable inside the method and return it.

Upvotes: 0

Views: 4102

Answers (1)

Bonatti
Bonatti

Reputation: 2781

If the shown code is indeed working (up to the log jsonResponse.toString()), you may do as shown here

Upvotes: 4

Related Questions