AsyncTask仍然阻止UI(UI没有响应)

时间:2015-10-10 16:13:07

标签: android android-asynctask android-volley

我有一个新闻应用程序,我可以获取一些XML和API并立即插入数据库。我在获取/插入数据库进程期间使用AsyncTask。但是我仍然得到一个块(UI不响应)

for (int i = 0; i < urls.size(); i++) {
    MyAsyncTask myAsyncTask =  new MyAsyncTask(urls.get(i));
    myAsyncTask.execute();
}

AsyncTask类:

  private class MyAsyncTask extends AsyncTask<Void,Void,Void> {

    Url mUrl;
    public MyAsyncTask(Url url){
        this.mUrl = url;
    }


    @Override
    protected Void doInBackground(Void... params) {
        connect(mUrl.getUrl(),mUrl.getSourceName(),mUrl.getChoice(),mUrl.getId());
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        whenToInflate[mUrl.getId()] = true;
        mProgressState += 15;
        mProgress.setProgress(mProgressState);

        if (areAllTrue(whenToInflate)) {
            inflate(1907);
            Arrays.fill(whenToInflate, false);
        }
    }
}

连接方法:

private void connect(String url, final String sourceSite, final int sourceChoice, final int i) {


    stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        if (sourceChoice == 14) {
                            database.insertFixtures(response, "FOOTBALL");
                        } else if (sourceChoice == 10 || sourceChoice == 20) {
                            database.insertNews(response, sourceChoice);
                        } else {
                            if (sourceChoice == 1) response = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
                            database.insertNewsXML(response, sourceSite, sourceChoice);
                        }


                    } catch (IllegalStateException | IllegalArgumentException | UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    });

    VolleySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
}

VolleySingleton课程:

public class VolleySingleton {
    private static VolleySingleton instance;
    private static VolleySingleton ourInstance = new VolleySingleton();
    private RequestQueue requestQueue;

    private VolleySingleton() {
    }

    private VolleySingleton(Context context) {
        requestQueue = Volley.newRequestQueue(context);
    }

    public static VolleySingleton getInstance() {
        return ourInstance;
    }

    public static VolleySingleton getInstance(Context context) {
        if (instance == null) {
            instance = new VolleySingleton(context);
        }
        return instance;
    }

    public RequestQueue getRequestQueue() {
        return requestQueue;
    }


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

2 个答案:

答案 0 :(得分:1)

这是因为您正在更新mProgress的进度。这就是为什么它会给出错误。更新onProgressUpdate方法AsyncTask。

中的进度

答案 1 :(得分:0)

看起来很奇怪你的项目在AsyncTask中使用了Volley,我之前从未做过这样的事情。

我建议您在以下问题中阅读我的答案供您参考:

Android Volley: Static vs Object

POST Request Json file passing String and wait for the response Volley

您会在其中使用Interface作为以下内容:

public interface VolleyResponseListener {
    void onError(String message);

    void onResponse(Object response);
}

然后我传递VolleyResponseListener作为方法的参数,在您的情况下是connect方法。

回到你的问题,IMO,你只需要在你的主线程中调用你的Volley请求(我的意思是stringRequest = new StringRequest...直接在onCreate里面调用)

希望这有帮助!