0

这是我的代码。我已经在谷歌上搜索了大约 2 个小时,我快疯了。我一直在做与网站上所有示例完全相同的操作,但它不会显示,也不会给我任何错误。

我正在尝试在 asyncTask 中检索网站源,我想显示对话框屏幕,并在完成后将其关闭。

这是我的代码:

public static class RetrieveSiteData extends AsyncTask<String, Void, String> {
    private Context SubContext;
    private ProgressDialog pd;

    public RetrieveSiteData(Context SubContext){
        this.SubContext = SubContext;
    }

    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(this.SubContext);
        pd.setTitle("Loading. Please wait...");
        pd.show();
        Log.v("Test","dit is het begin");
    } 


    @Override
    protected String doInBackground(String... urls) {
        StringBuilder builder = new StringBuilder(1000000);


        for (String url : urls) {
            Log.v("URLS",url);
            if(url != "" && url != null){
                try {                   
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet(url);
                    HttpResponse response = client.execute(request);

                    InputStream in = response.getEntity().getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String line = null;
                    while((line = reader.readLine()) != null)
                    {
                        builder.append(line);
                    }
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


        return builder.toString();
    }


    @Override
    protected void onPostExecute(String result) {
        pd.dismiss();
        Log.v("Test","dit is het eind");
    }
}
4

3 回答 3

0

尽量不要调用 AsyncTask 的 get()。它将阻塞 ui 线程,直到 AsyncTask 完成,因此没有显示 ProgressDialog。

于 2013-06-01T11:12:29.167 回答
0

只需输入您的活动名称

@Override
protected void onPreExecute() {
    pd = new ProgressDialog(Youractivityname.this);
    pd.setTitle("Loading. Please wait...");
    pd.show();
    Log.v("Test","dit is het begin");
} 
于 2013-06-01T11:22:25.973 回答
0

尝试这样使用

 @Override
    protected void onPreExecute() {

        pd = ProgressDialog.show(SubContext, "", "Loading. Please wait...", true);

        Log.v("Test","dit is het begin");
    } 
于 2013-06-01T10:31:16.133 回答