小编chi*_*ean的帖子

等待Async Volley请求的结果并将其返回

下面是一个方法,我试图通过调用getSelf()来检索用户对象.问题是结果始终为null,因为Volley请求在返回结果时尚未完成.我对异步进程有些新意,所以我不确定让方法等待API调用结果返回UserBean对象的最佳方法.谁能给我一些帮助?

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:", …
Run Code Online (Sandbox Code Playgroud)

java android asynchronous anonymous-function android-volley

3
推荐指数
2
解决办法
1万
查看次数