简体   繁体   English

如何从Volley onResponse返回解析的数据?

[英]How to return parsed data from Volley onResponse?

I am using Volley StringRequest to make GET call to web services using following code. 我正在使用Volley StringRequest使用以下代码对Web服务进行GET调用。

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 然后在onResponse

datalistener.onDataReceived(list);    

In your case you have a list defined just outside the Volley request. 在您的情况下,您仅在Volley请求之外定义了一个列表。 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. 但是,您使用的是StringRequest,并且只能从服务器获取String作为响应。 You will have to parse the String yourself and build up the list. 您将必须自己解析String并建立列表。

If you are expecting a JSON respons you can use JsonArrayRequest or JsonObjectRequest instead of StringRequest. 如果期望JSON响应,则可以使用JsonArrayRequest或JsonObjectRequest代替StringRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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