How can I return value from function onResponse of Volley?
Asked Answered
N

3

62
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?

Nepotism answered 23/1, 2015 at 22:50 Comment(0)
S
154

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

Squeal answered 23/1, 2015 at 23:8 Comment(3)
@morha13 just declare the variable callback final in the method signature, since you won't be changing it (at least in this basic case): public void getString(final VolleyCallback callback). I have changed the answer to reflect this.Squeal
can someone give me an example for a volley callback?Rutledge
but in the first place when called getString, What will be returned ? because volley request keep some time ?Occam
F
0
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'

Folklore answered 13/3, 2020 at 14:23 Comment(0)
I
0

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.

Imprecise answered 10/1, 2022 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.