函数调用后最终对象丢失属性值

时间:2017-10-02 18:18:41

标签: java android android-volley final

我正在尝试在我的Android应用程序中正确处理Volley响应,该应用程序从数据库加载一些项目。排球功能封装在WebRequester类:

public class WebRequester extends Application {
    public static final String TAG = WebRequester.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private static WebRequester mInstance;

    public WebRequester() {
        mInstance = this;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public static synchronized WebRequester getInstance() {
        return mInstance;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    /* ... */
}

另一个类ItemsController集中了要创建的请求。为了获得响应代码,我创建了一个嵌套类VolleyCallback,并在覆盖responseCode调用中设置其属性parseNetworkResponse()

public class FeedItemsController extends Application {
    private String URL_GET_FEED_ITEMS = /* My URL */; 
    private static final String TAG = FeedItemsController.class.getSimpleName();

    private ArrayList<FeedItem> feedItems;

    public class VolleyRequestCallback {
        public int responseCode;

        public int getResponseCode() {
            return responseCode;
        }

        public void setResponseCode(int responseCode) {
            this.responseCode = responseCode;
        }
    }

    public void loadItems() {
        final VolleyRequestCallback callback = new VolleyRequestCallback();

        if (feedItems == null) {
            feedItems = new ArrayList<>();

            Cache cache = WebRequester.getInstance().getRequestQueue().getCache();
            Cache.Entry entry = cache.get(URL_GET_FEED_ITEMS);

            if (entry != null) {
                try {
                    String data = new String(entry.data, "UTF-8");
                    parseJsonFeed(new JSONObject(data));
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET, URL_GET_FEED_ITEMS, null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                VolleyLog.d(TAG, "Response: " + response.toString());
                                parseJsonFeed(response);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d(TAG, "Error: " + error.getMessage());
                            }
                         }
                ) {
                    @Override
                    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                        callback.setResponseCode(response.statusCode);
                        System.out.println("Code 1 = " + response.statusCode); 
                        return super.parseNetworkResponse(response);
                    }
                };

                WebRequester.getInstance().addToRequestQueue(jsonReq);
            }
        }
        System.out.println("Code 2 = " + callback.getResponseCode());
    }

    /* ... */
}

然后从另一个类调用方法loadItems()。问题是 - 当它进入parseNetworkResponse()方法时,resultCode被正确设置为,比方说200.但是,当我尝试在请求覆盖之外重用它时,它是0再次:

Code 1 = 200
Code 2 = 0

这可能是响应处理的错误实现,但我的主要问题是为什么对象属性发生了变化?

提前致谢

1 个答案:

答案 0 :(得分:0)

原来这是一个不令人兴奋的错误。对parseNetworkResponse的调用是异步的,这意味着当执行第一次打印时,服务器尚未响应。