1

How do I get the response out of onResponse so that I can return it? I have found this but I don't really understand how to implement it for me. How can I return value from function onResponse of Volley?

I hope there is an updated method or something easier that I might have missed This is my code

public Boolean checkIfPersonExists(Context context, final Person aPerson){
        StringRequest postRequest = new StringRequest(Request.Method.POST, USEREXISTS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("RESPONSE",response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                params.put("email",aPerson.getEmail());
                params.put("phone",aPerson.getPhone());
                return params;
            }
        };
        Volley.newRequestQueue(context).add(postRequest);
        return null;
    }
Community
  • 1
  • 1
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • 1
    You will return nothing. But call a function with `response` as parameter. That functions handles the ... response. – greenapps Dec 03 '16 at 14:20
  • But I can't call another function as checkIfPersonExists is called from an Activity. checkIfPersonExists sits in another class. – JianYA Dec 03 '16 at 14:25
  • You should implement that as an interface as described in the link which you do not understand. – greenapps Dec 03 '16 at 14:30

1 Answers1

0

As it was mentioned, you need to implement interface and get a callback

Create a new interface like this

public interface GeneralCallbacks {
void VolleyResponse(String data);
}

Implement the interface on your activity class

public class YourActivity implements ScoreboardCallback
{
    @Override
    public void VolleyResponse(String data)
    {
      //do stuff with data
    }
}

And finally changing your response function to something like this

        @Override
        public void onResponse(String response) {
            ((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
            aPerson.SomeFunction(); // This will call person's function
            Log.d("RESPONSE",response.toString());
        }
P.C. Blazkowicz
  • 489
  • 1
  • 4
  • 11
  • Sorry just to clarify, can you show me an example of the code being called elsewhere? How do I call the response out if it needs a parameter? – JianYA Dec 03 '16 at 22:47
  • Ive fixed some error on the code. So, Do you want to make a call to aPerson or callback to activity? or where? – P.C. Blazkowicz Dec 04 '16 at 12:23