How to pass the response received by the volley to a Global variable?

1

Good afternoon, I wanted to know how I can pass%% of the received volley to a global variable:

public class ReceberObjeto {
    private JSONObject res;

    public ReceberObjeto(String URL, Context context, Map<String,String> params) {

        RequestQueue requestQueue = Volley.newRequestQueue(context);

        CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, URL, params,
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject response) {
                    res = response; //AQUI ESTA O PROBLEMA
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    volleyError.printStackTrace();
                }
            });
        requestQueue.add(request);
    }
}
    
asked by anonymous 13.05.2016 / 21:16

2 answers

2

I think what you want is for this class to communicate the result to the class that uses it.

The use of global variables is not recommended , there are other ways to get what you want.

A usual way is this class to receive / register a Listener that has a method that will be called when the result is retrieved. Note that the CustomJsonObjectRequest class itself uses this system to inform the client code when it gets the Response or there is an error (methods onResponse() and onErrorResponse() of interface Response.Listener ).

Start by declaring an interface that Listener should implement:

public interface ResultListener {
      public void onResult(JSONObject resultado);
}  

Create a method to receive / register Listener :

public void setResultListener(ResultListener listener){
    this.listener = listener;
}

When the result is obtained call the interface method, passing the result:

listener.onResult(response);  

All that code that is in the constructor must be passed to a method of its own. The constructor should only have code if it is to set the state of the instance.
Creating a read method will allow you to read new objects without having to create a new instance of the class.

I also do not "like" the class name, perhaps ObjectLoader is better.

Making changes will look like this:

public class ObjectLoader {

    public interface ResultListener {
          public void onResult(JSONObject result);
    }  

    private ResultListener listener;

    public void Load(String URL, Context context, Map<String,String> params) {

        RequestQueue requestQueue = Volley.newRequestQueue(context);

        CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, URL, params,
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject response) {
                    //O resultado foi obtido
                    //Informe o listener
                    listener.onResult(response);
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    volleyError.printStackTrace();
                }
            });
        requestQueue.add(request);
    }

    public void setResultListener(ResultListener listener){
        this.listener = listener;
    }
}

To use the class do so:

ObjectLoader objectLoader = new ObjectLoader();

objectLoader.setResultListener(new ResultListener(){
    @Override
    public void onResult(JSONObject result) {

        //Utilize aqui o resultado
    }
});
objectLoader.load(URL, context, params);
    
14.05.2016 / 13:09
0

In this case, simply make the object static, and it will be "global".

private static JSONObject res;
    
13.05.2016 / 22:30