简体   繁体   中英

How to return parsed data from Volley onResponse?

I am using Volley StringRequest to make GET call to web services using following code.

ArrayList<HashMap<String, Object>> list;
StringRequest getRequest = new StringRequest(Request.Method.GET,activity.getString(R.string.URL),
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    int success;

                //Parsed data and Want to return list 
                //return list;

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("Error.Response", error.toString());


                }
            });
    request.add(getRequest);

Any idea how do I return the list data?

Use interface as listeners and return the data

public interface DataListener {

void onDataReceived(ArrayList<HashMap<String, Object>> response);
void onError(DataResponse resp); }    

then in onResponse

datalistener.onDataReceived(list);    

In your case you have a list defined just outside the Volley request. Just assign the new data from where you receive the data.

list = yourNewList;

or use interfaces.

However you are using a StringRequest and you will only get a String as a response from the server. You will have to parse the String yourself and build up the list.

If you are expecting a JSON respons you can use JsonArrayRequest or JsonObjectRequest instead of StringRequest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM