排球锁定GUI线程?

时间:2015-04-19 21:56:31

标签: android android-asynctask android-volley

我遇到了Volley的问题,我的应用程序是"锁定"当一个Volley请求需要很长时间才能完成。我觉得这应该是一个非常普遍的问题,但我还没有找到解决问题的方法。

当响应很快时,一切都运行良好......但是当响应很长时,我一直看到跳帧。

认为我已经用我的调试器验证了创建了一个单独的线程,创建了对象,然后正确地销毁了自己。所以,只是离开"什么阻碍主线程?"

I/Choreographer﹕ Skipped 297 frames!  The application may be doing too much work on its main thread.
I/MyApp: got response
D/Volley﹕ [1] Request.finish: 5008 ms: [ ] RequestURL

这是我的代码:

public class MyDataLoader {

    MyAsyncProcessor processor;

    public MyDataLoader() {
        processor = new MyAsyncProcessor();
    }

    public void fetchData(String url) {
        RequestQueue queue = Volley.newRequestQueue(activity);

        JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {

                    // Function that's called when a response is received
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i("MyApp", "got response");
                        processor.execute(response);
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Do error stuff
            }
        });
// Add the request to the RequestQueue.
        queue.add(getRequest);
    }

    private class MyAsyncProcessor extends AsyncTask<JSONObject, Integer, DataObject> {

        @Override
        protected DataObject doInBackground(JSONObject... params) {
            //Process json here
            DataObject myDataObject = new DataObject(params[0]);
            return myDataObject;
        }

        @Override
        protected void onPostExecute(DataObject myDataObject){
            //Tell the activity we're ready for display
        }
    }
}

0 个答案:

没有答案