Android AsyncTask get函数未显示Progressbar

Android AsyncTask get函数未显示Progressbar,android,android-asynctask,Android,Android Asynctask,我正在制作一个应用程序,它从一个网站获取所有html代码,然后根据我的要求提取数据。当异步在后台工作以获取数据时,我需要一个进度对话框,在列表视图中显示数据后,进度栏应该关闭。我在谷歌上搜索了许多问题和教程。问题是我不能在这里使用get函数,因为我的对话框没有显示。所以在那之后,我搜索了谷歌的另一个方法,说我应该使用回调方法。但我需要doInBackground方法的数据。如果不使用get方法,如何获取数据 这是我的密码: public class MainActivity extends Ap

我正在制作一个应用程序,它从一个网站获取所有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);


}

}
public类MainActivity扩展了AppCompatActivity{
列表视图列表视图;
ArrayList myArray;
ArrayList myImageArray;
进程对话进程;
字符串获取数据;
公共类DownloadTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progress.setMessage(“下载音乐:)”;
progress.setProgressStyle(ProgressDialog.STYLE_水平);
progress.setUndeterminate(true);
progress.show();
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
进步。解散();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
字符串结果=”;
URL=新URL(参数[0]);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(inputStream);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
getData=结果;
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回“失败!”;
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
字符串总数=”;
字符串newTotal=“”;
listView=(listView)findViewById(R.id.listView);
myArray=newArrayList();
myImageArray=newArrayList();
进度=新建进度对话框(此对话框);
DownloadTask任务=新建DownloadTask();
试一试{
字符串文本=任务。执行(“https://www.earticleblog.com/).get();
模式p=Pattern.compile((.*);
匹配器m=p.Matcher(文本);
while(m.find()){
总+=m组(1);
}
p=Pattern.compile(“a href=\”(.*?\“>”);
m=p.matcher(总计);
while(m.find()){
新总数+=m组(1);
}
p=Pattern.compile(“title=\”(.*http”);
m=p.matcher(新总数);
while(m.find()){
myArray.add(m.group(1));
myImageArray.add(R.drawable.tryit);
}
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
ArrayAdapter=新的CustomAdapter(this,myImageArray,myArray);
setAdapter(适配器);
}
}

您必须在onPreExecute()方法中启动进度条,并且必须在onPostExecute()方法中关闭进度条

您需要以这种方式显示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!";

        }


    }

}


试试这个方法。

改变下面两种方法

    @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");
    }

如前所述,您应该在
onPreExecute
方法中显示
ProgressDialog
,并隐藏在
onPostExecute()。
此外,使用
ProgressDialog=new ProgressDialog(this);
只创建对话框,不显示。尝试
ProgressDialog.show(…)
()


另外,还有一个提示-在
onCreate
中不要做太多事情(尤其不要做任何循环)-您的应用程序将延迟显示。

但当我执行此操作时,进度对话框仍不显示,请应用我的上述解决方案。您只需在预执行方法中写一行,首先检查是否可以看到对话框,然后尝试应用样式和所有样式,同时删除进度=新建进度对话框(此)我遇到的问题是我正在使用get方法,因为它我的对话框没有显示我必须用回调方法替换它,但不知道如何操作,但是当我执行此操作时,进度对话框仍然没有显示,但是当我执行此操作时,进度对话框仍然没有显示在异步类中,您可以看到没有进度条初始化并请查看我的更新答案@AnshumanKaushik问题是你没有调用异步任务,所以你需要像此任务一样调用任务。execute();@AnshumanKaushika应用我的解决方案,然后检查你是否得到解决方案?不要这样使用AsyncTask。只使用execute(参数)和task之后的所有内容。execute()移动到onPostExecute()。您正在尝试下载主线程中的数据,然后立即使用它。只需在AsyncTask上调用execute,然后onCreate应该结束。当AsyncTask完成时,它将调用onPostExecute,并在那里使用您的结果。实际上我正在考虑它。我将尝试此操作,并会让您知道,谢谢兄弟,在我放入所有代码后,它会工作在onPostMethod中
new Downloadtask().execute();
    @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");
    }