1

I have tried the method from Android Volley return value from response but I keep getting this error : com.box.ajian.box.Controllers.IOFunctions cannot be cast to com.box.ajian.box.Constants.VolleyCallback . What should I put in my casting area for it to work? I am quite confused with tis volleycallback stuff.

This is my code. Hope someone can help me

IOFunctions.java (Not an activity)

public void checkIfPersonExists(final 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());
                ((VolleyCallback)context).onSuccess(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);
    }

Functions.java (Not an activity)

public Boolean register(Person aPerson,Context context){
        ioFunctions.checkIfPersonExists(context,aPerson);
        new VolleyCallback(){
            @Override
            public void onSuccess(String result) {
                if(result.equals(false)){
                    Log.d("HELLO","HELLO");
                }
            }
        };
    return false;
}

Register.java (Activity)

 functions.register(new Person(Firstname,Lastname,functions.dateFormatter(DateofBirth),Email,Password,Phone),Register.this);

Interface

public interface VolleyCallback {
    void onSuccess(String result);
}

The activity register calls the functions.java which then calls iofunctions.java. I want iofunctions.java to return a true or false and so Im relying on volley.

Community
  • 1
  • 1
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

0

You do not need your own callback class. Volley comes with it's own!

Just add it to the parameters, pass it to the request.

public void checkIfPersonExists(final Context context, final Person aPerson, 
    Response.Listener<String> onResponse){ // <-- here
        StringRequest postRequest = new StringRequest(Request.Method.POST, USEREXISTS, 
            onResponse, // <--- here
            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);
}

Then handle accordingly, but don't make this method return because Volley is asynchronous.

public void register(Person aPerson,Context context){
    ioFunctions.checkIfPersonExists(context,aPerson, 
        new Response.Listener<String>(){
            @Override
            public void onResponse(String response) {
                boolean exists = !result.equals("false");
                if(!exists){
                    Log.d("Person exists","Nope!");
                    // Person does not exist... continue registration from here
                } else {
                    Log.d("Person exists", "That account exists!");
                }
            }
        };
    // Don't return here
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for answering. But I want to get the response out of the function. I want to return it to another function – JianYA Dec 04 '16 at 01:45
  • You can't use `return`. That's my point. You have to use callbacks. If you need the response from the `register` function, then you have to add another parameter for one – OneCricketeer Dec 04 '16 at 01:55
  • Really, though, you should make the backend perform the "user exists" function as part of the "register" process. Having the client be responsible for both network calls is bad on the battery – OneCricketeer Dec 04 '16 at 01:56
  • I see. Thank you! – JianYA Dec 04 '16 at 01:57
  • If you are doing a lot of requests like this, you may find that Retrofit & RxJava make a nice combination – OneCricketeer Dec 04 '16 at 02:00