等待的异步凌空请求结果,并将其返回 [英] Wait for result of Async Volley request and return it

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

问题描述

下面是一种方法,而我试图通过调用getSelf来检索用户对象()。问题是,结果总是空,因为排球请求不返回结果的时间完成。我有点新的异步处理,所以我不肯定有方法等待API调用返回的UserBean对象的结果的最佳途径。任何人都可以给我一些帮助吗?

 公开的UserBean getSelf(字符串URL){

    RpcJSONObject jsonRequest =新RpcJSONObject(getSelf,新JSONArray());

    JsonObjectRequest userRequest =新JsonObjectRequest(Request.Method.POST,网址,jsonRequest,
        新Response.Listener<的JSONObject>(){
            @覆盖
            公共无效onResponse(JSONObject的响应){

                字符串结果;
                尝试 {
                    结果= response.getString(结果);
                    GSON GSON =新GSON();
                    java.lang.reflect.Type listType =新TypeToken&其中;的UserBean>(){} .getType();

                    //如何退还该值通过父类的方法?
                    的UserBean =(的UserBean)gson.fromJson(结果,listType);

                }赶上(JSONException E){
                    e.printStackTrace();
                }

            }
        },新Response.ErrorListener(){
            @覆盖
            公共无效onErrorResponse(VolleyError错误){
               Log.e(错误,error.toString());
               完();
            }
        }
    );

    this.queue.add(userRequest);


    返回的UserBean;

}
 

解决方案

对于那些即将从搜索和这个问题;谷歌。

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

 字符串URL =htt​​p://www.google.com/humans.txt;

RequestFuture<字符串>未来= RequestFuture.newFuture();
StringRequest请求=新StringRequest(Request.Method.GET,网址,未来,未来)
mRequestQueue.add(要求);

字符串结果=的Future.get(); //这条线将阻止
 

请记住,你必须运行阻塞code在另一个线程,所以把它包装成的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).

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

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