等待 Async Volley 请求的结果并返回它 [英] Wait for result of Async Volley request and return it

查看:25
本文介绍了等待 Async Volley 请求的结果并返回它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我试图通过调用 getSelf() 来检索用户对象的方法.问题是结果始终为空,因为在返回结果时 Volley 请求尚未完成.我对异步进程有点陌生,所以我不确定让方法等待 API 调用的结果返回 UserBean 对象的最佳方法.有人可以帮我吗?

public UserBean getSelf(String url){RpcJSONObject jsonRequest = new RpcJSONObject("getSelf", new JSONArray());JsonObjectRequest userRequest = new JsonObjectRequest(Request.Method.POST, url, jsonRequest,新的 Response.Listener() {@覆盖公共无效onResponse(JSONObject响应){字符串结果;尝试 {result = response.getString("result");Gson gson = new Gson();java.lang.reflect.Type listType = new TypeToken() {}.getType();//我如何通过父方法返回这个值??userBean = (UserBean) gson.fromJson(result, listType);} catch (JSONException e) {e.printStackTrace();}}}, 新的 Response.ErrorListener() {@覆盖公共无效onErrorResponse(VolleyError错误){Log.e("错误:", error.toString());结束();}});this.queue.add(userRequest);返回用户Bean;}

解决方案

对于那些从 search & 提出这个问题的人谷歌.

没有理由等待异步请求完成,因为它是异步设计的.如果你想使用 Volley 实现同步行为,你必须使用所谓的futures:

String url = "http://www.google.com/humans.txt";RequestFuture未来 = RequestFuture.newFuture();StringRequest request = new StringRequest(Request.Method.GET, url, future, future)mRequestQueue.add(request);字符串结果 = future.get();//这行会阻塞

请记住,您必须在另一个线程中运行阻塞代码,因此将其包装到 AsyncTask 中(否则 future.get() 将永远阻塞).

Below is a method in which I am trying to retrieve an user object by calling getSelf(). Problem is that the result is always null since the Volley request has not finished at the time of returning the result. I'm somewhat new to async processes, so I am not sure of the best way to have the method wait for the result of the API call to return the UserBean object. Can anyone give me some help?

public UserBean getSelf(String url){

    RpcJSONObject jsonRequest = new RpcJSONObject("getSelf", new JSONArray());

    JsonObjectRequest userRequest = new JsonObjectRequest(Request.Method.POST, url, jsonRequest, 
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                String result;
                try {
                    result = response.getString("result");
                    Gson gson = new Gson();
                    java.lang.reflect.Type listType = new TypeToken<UserBean>() {}.getType();

                    //HOW DO I RETURN THIS VALUE VIA THE PARENT METHOD??
                    userBean = (UserBean) gson.fromJson(result, listType);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               Log.e("Error:", error.toString());
               finish();
            }
        }
    );

    this.queue.add(userRequest);


    return userBean;

}   

解决方案

For those coming to this question from search & google.

There is no reason to wait for an async request to finish, as it is asynchronous by design. If you want to achieve synchronous behaviour using Volley, you have to use so-called futures:

String url = "http://www.google.com/humans.txt";

RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Request.Method.GET, url, future, future)
mRequestQueue.add(request);

String result = future.get(); // this line will block

Keep in mind that you have to run blocking code in another thread, so wrap it into AsyncTask (otherwise future.get() will block forever).

这篇关于等待 Async Volley 请求的结果并返回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆