在AsyncTask Android应用程序的PostExecute方法中显示Toast消息

时间:2014-06-02 18:09:36

标签: java android listview android-asynctask

好的,我有一个AsyncTask正在向ListView添加项目。在PreExecute上,我正在显示一条消息" Starting"在PostExecute我正在显示"完成"。 "开始"显示消息但由于某种原因,当列表完成时,"完成"消息未显示。有没有人知道为什么邮件没有显示在此任务的PostExecute部分?谢谢

代码:

class FillLocations extends AsyncTask<Integer, Void, Void> {

    public FillLocations() {

    }

    protected void onPreExecute() {
        Toast.makeText(getApplicationContext(), "Starting",
                Toast.LENGTH_LONG).show();
    }

    // Decode image in background.
    @Override
    protected Void doInBackground(Integer... params) {

        String result = "";
        InputStream isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myscript/"); // YOUR
                                                                                // PHP
                                                                                // SCRIPT
                                                                                // ADDRESS
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
            // resultView.setText("connected");
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(isr, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error  converting result " + e.toString());
        }

        // parse json data
        try {
            JSONArray jArray = new JSONArray(result);

            for (int i = 0; i < jArray.length(); i++) {
                final JSONObject json = jArray.getJSONObject(i);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        try {

                            BitmapWorkerTask myTask = new BitmapWorkerTask(
                                    json.getInt("ID"), json);
                            myTask.execute();

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });

            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data " + e.toString());
        }
        return null;

    }

    protected void onPostExecute() {
        Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG)
                .show();

    }
}

2 个答案:

答案 0 :(得分:0)

您需要正确实施onPostExecute

@Override
protected void onPostExecute(Void void){
      // Do what you want to do, for example, your toast:
      Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG)
            .show();
}

-Notes on General Practice-

您的AsyncTask不需要构造函数,因此您可以删除

public FillLocations(){
}

你应该声明你的类可见性(除非你希望它设置为默认的可见性,这是​​包),所以让它像

private class FillLocations extends AsyncTask<Integer, Void, Void>{
    // Your Stuff
}

答案 1 :(得分:0)

onPostExecute采用如here所示的参数。

就我个人而言,我会做这样的事情,告诉用户出现了什么问题,万一引发了一些异常。我从不喜欢让错误发生在必杀技。

protected String doInBackground(Integer... params) {
...
}

protected void onPostExecute(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG)
            .show();

}

(另外,在文档中,没有使用@Override。)