How can I return value from function onResponse of Volley?

AndroidAndroid Volley

Android Problem Overview


public class getString  {
String tag_string_req = "string_raq";
String url = "http://10.0.2.2/eat/locations/index.json";
String result="";

public String get_String() {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            result=response;
            System.out.println(response);
            ;

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            System.out.println(volleyError.getMessage());
        }
    });
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    return result;
}}

I would to build an object of getString and call get_String in other fields. But it seems that it is hard to get the result out from the onResponse. I know it cannot work in this current way. Could anyone help me to settle this problem?

Android Solutions


Solution 1 - Android

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()).

Solution 2 - Android

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {


    val rootView = inflater.inflate(R.layout.mission_fragment_list, container, false)
    val recycler = rootView.findViewById(R.id.list) as RecyclerView

    GlobalScope.async {
        val queue = Volley.newRequestQueue(context)
        val gson = Gson()

        val url = "YPUR_API"
        val stringRequest = StringRequest(
            Request.Method.GET, url,
            Response.Listener<String> { response ->

                recycler.layoutManager = LinearLayoutManager(context)
                val listType: Type = object : TypeToken<MutableList<MissionModel>?>() {}.type
                val data: MutableList<MissionModel> = gson.fromJson(response, listType)
                recycler.adapter = MissionAdapter(data)
            },
            Response.ErrorListener { error ->
                error.printStackTrace()
            })

        queue.add(stringRequest)


    }
    return rootView

}

Simple code. you need to add dependency implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'

Solution 3 - Android

As wblaschko a very good solution has mentioned, I would understand that this solution might be a bit confusion for those who have a bit less experience. Underneath my edited solution on top of wblaschko's solution:

VolleyCallback.java (Interface)

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

I made a HTTPRequest class to keep things clean.

HTTPReq.java (Class)

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class HTTPReq {
    public static StringRequest getRequest(String path, final VolleyCallback callback) {
        // Instantiate the RequestQueue.
        String url = "https:// **FILL HERE BASE API URL** /" + path;

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        callback.onSuccess(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.onError(error.toString());
            }
        });

        return stringRequest;
    }
}

And as last, in the activity, you want to implement the following code:

Create a global variable:

private RequestQueue mQueue;

After that, apply in the onCreate function of a activity a VolleyRequestQue:

mQueue = Volley.newRequestQueue(this);

And as last, the function which actually performs the API request and can catch the response:

mQueue.add(HTTPReq.getRequest( **FILL WITH PATH AND/OR PARAMS**, new VolleyCallback() {
    @Override
    public void onSuccess(String result) {
        System.out.println(result);
    }

    @Override
    public void onError(String result) {
        System.out.println(result);
    }
}));

Hope this makes more clear.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWubin OuyangView Question on Stackoverflow
Solution 1 - AndroidwblaschkoView Answer on Stackoverflow
Solution 2 - AndroidReign YaduvanshiView Answer on Stackoverflow
Solution 3 - AndroidKoen van der marelView Answer on Stackoverflow