How can I return value from function onResponse of Volley?

You want to use callback interfaces like so:

public void getString(final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new     Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            ...  // (optionally) some manipulation of the response 
            callback.onSuccess(response);
        }
    }...
}}

Where the callback is defined as

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

Example code inside activity:

public void onResume(){
    super.onResume();

    getString(new VolleyCallback(){
         @Override
         public void onSuccess(String result){
             ... //do stuff here
         }
    });
}

You can also make VolleyCallback more robust, using generic types if you want to do processing, or adding start(), failed(Exception e), complete(), etc methods to do a little more fine-grained state checking.

Keep in mind this is an async call, so you will have to update views, etc when you get the result back (inside success()).

Leave a Comment