How to pass the answer received by Volley to a Global variable?

Asked

Viewed 159 times

1

Good afternoon, I was wondering how I could spend one JSONObject received in Volley for 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);
    }
}
  • Why you want to use a global variable?

  • This data that I will receive will be returned in the getRes method to another class. However I am not getting, when I return, the value is null

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.

One usual way is for this class to receive/register a Listener which has a method that will be called when the result is obtained.
Notice that the class itself Customjsonobjectrequest uses this system to inform the client code when it obtains the Response or there is some error(methods onResponse() and onErrorResponse() of the interface Response.Listener).

Start by declaring an interface that Listener must 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 its own method.
The constructor should only have code if it is intended to define the state of the instance.
Creating a readable method will allow you to read new objects without creating a new instance of the class.

Also not "like" the class name, maybe Objectloader be better.

Making the 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);
  • 1

    great explanation, that’s exactly what I needed, implemented and worked perfectly! Thanks for the help.

0

In this case, just make the Object static, and it will be "global".

private static JSONObject res;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.