I have trouble understanding VOLLEY.

public void JsonArrayRequest(){ shops = new ArrayList<>(); request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }); queue.add(request); } 

Logically, I call this request from somewhere above, from the layer with logic, I want to return the response from its working out and parse it, and then return it to the drawing. The problem is that I can only work with response from the onResponse method. I tried to: Announce another JSONArray in the class and map this response there and getter to it, but as soon as I do something with it not from onResponse, it is empty (. How to take this response correctly and give it to another layer?

  • The question is that the request is executed asynchronously, and you can use its result only when the answer is received - onResponse is called, respectively, the logic of working with the result should be run from it, and not immediately after sending the request. - woesss pm

1 answer 1

In the JsonArrayRequest method JsonArrayRequest send a callback. And in onResponse call this callback and send data there.