AsyncTask对话框未被解雇

时间:2015-05-29 01:20:18

标签: java android android-asynctask android-dialog

我正在使用异步任务获取一些数据,但它始终显示在屏幕上,从不关闭或隐藏。为什么呢?

public class getJson extends AsyncTask<String,Void,String>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(kategori.this);
        pDialog.setMessage("Yükleniyor...");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false); // ProgressDialog u iptal edilemez hale getirdik.
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... unused) {
        String resString = "";
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://*********.com/api.asp?cmd=katdok&ID="+talepKategori);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity;
            entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line+ "\n");
            }
            resString = sb.toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return resString;
    }

    @Override
    protected void onPostExecute(String string) {
        super.onPostExecute(string);
        pDialog.dismiss();
    }
}

2 个答案:

答案 0 :(得分:0)

我正在使用以下方法确保在任务完成后解除Dialog,

$(this).find('>NAME').text() //Not the >

它避免获取null并强行关闭对话框。

答案 1 :(得分:0)

尝试以下代码

public class getJson extends AsyncTask<String,Void,String>
{
    ProgressDialog progressDialog = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(kategori.this,
                    getResources().getText(R.string.app_name), getResources()
                    .getText(R.string.wait_message), true);
    }

    @Override
    protected String doInBackground(String... unused) {
        String resString = "";
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://*********.com/api.asp?cmd=katdok&ID="+talepKategori);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity;
            entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line+ "\n");
            }
            resString = sb.toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return resString;
    }

    @Override
    protected void onPostExecute(String string) {
        super.onPostExecute(string);
        if(progressDialog.isShowing())
                progressDialog.dismiss();
    }
}