AsyncTask get函数未显示Progressbar

时间:2016-06-18 11:54:52

标签: android android-asynctask

我正在制作一个从网站上获取所有HTML代码的应用程序,然后根据我的要求提取数据。我想要一个进度对话框,当异步在后台工作以获取数据并在列表视图中显示数据后进度条应该解雇。我在谷歌搜索了很多问题和教程。问题是我不能在这里使用get函数,因为我的对话框没有显示。所以在那之后我搜索谷歌另一种方法,说我应该使用回叫方法。但我想要来自doInBackground方法的数据。如果我不使用get方法,我怎样才能获得数据。

这是我的代码:

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayList<String> myArray;
ArrayList<Integer> myImageArray;
ProgressDialog progress;
String getData;

public class DownloadTask extends AsyncTask<String,Void,String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

     progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     progress.setIndeterminate(true);
    progress.show();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
     progress.dismiss();

    }

    @Override
    protected String doInBackground(String... params) {

        try{

            String result= "";

            URL url = new URL(params[0]);

            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

            InputStream inputStream = urlConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(inputStream);

            int data = reader.read();

            while (data!=-1){

                char current = (char)data;

                result += current;

                data = reader.read();
            }

            getData = result;

            return result;

        }catch (Exception e){

            e.printStackTrace();

            return "Failed!";

        }


    }

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String total= "";
    String newTotal = "";

    listView = (ListView)findViewById(R.id.listView);
    myArray = new ArrayList<String>();
    myImageArray = new ArrayList<Integer>();

    progress = new ProgressDialog(this);

    DownloadTask task = new DownloadTask();

    try {

        String text = task.execute("https://www.earticleblog.com/").get();

        Pattern p = Pattern.compile("<div class=\"td-module-thumb\">(.*?)</div>");

        Matcher m = p.matcher(text);

        while(m.find()){


          total += m.group(1);


        }

        p = Pattern.compile("a href=\"(.*?)\">");

        m = p.matcher(total);

        while (m.find()){

            newTotal += m.group(1);

        }

        p = Pattern.compile("title=\"(.*?)http");

        m = p.matcher(newTotal);

        while (m.find()){

            myArray.add(m.group(1));
            myImageArray.add(R.drawable.tryit);

        }

    } catch (InterruptedException e) {

        e.printStackTrace();

    } catch (ExecutionException e) {

        e.printStackTrace();

    }


    ArrayAdapter  adapter = new CustomAdapter(this,myImageArray,myArray);
    listView.setAdapter(adapter);


}

}

4 个答案:

答案 0 :(得分:1)

你需要这样做才能显示progressdailog。

在onPreExecute()中你需要像这样初始化进度

progres = new ProgressDialog(MainActivity.this);
progres.setMessage("Please Wait");
progres.show();

并在onPostExecute();你需要取消进度

if(progres.isShowing()){
progres.dismiss();
}
您的班级中的

只需复制并替换此

public class DownloadTask extends AsyncTask<String,Void,String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
       progres = new ProgressDialog(MainActivity.this);
       progres.setMessage("Please Wait");
       progres.show();

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

           if(progres.isShowing()){
                progres.dismiss();
             }

    }

    @Override
    protected String doInBackground(String... params) {

        try{

            String result= "";

            URL url = new URL(params[0]);

            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

            InputStream inputStream = urlConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(inputStream);

            int data = reader.read();

            while (data!=-1){

                char current = (char)data;

                result += current;

                data = reader.read();
            }

            getData = result;

            return result;

        }catch (Exception e){

            e.printStackTrace();

            return "Failed!";

        }


    }

}

调用您的异步任务

task.execute();

new Downloadtask().execute();

尝试这种方式。

答案 1 :(得分:1)

如上所述,您应该在ProgressDialog方法中显示onPreExecute,并隐藏在onPostExecute().中。此外,使用progress = new ProgressDialog(this);您只会创建对话框而不显示。试试ProgressDialog.show(...)https://developer.android.com/reference/android/app/ProgressDialog.html)。

而且,还有一个提示 - 在onCreate中不要做太多(特别是不做任何循环) - 你的应用会延迟显示。

答案 2 :(得分:0)

你必须在onPreExecute()中启动进度条;方法,你必须在onPostExecute();中关闭它;方法

答案 3 :(得分:0)

更改以下两种方法,

    @Override
    protected void onPostExecute(String result) {
        // execution of result of Long time consuming operation
        progressDialog.dismiss();

    }


    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(MainActivity.this,
                "", "Please Wait");
    }
相关问题