Android ProgressDialog

孤街浪徒 提交于 2019-12-12 03:27:40

问题


how can I display a ProgressDialog for this code?

 try{

            leggiNews = Pattern.compile("<p><a href='(.*?)' class='rossobig'>(.*?)</a><br/>(.*?)</p>"); 


            leggi = leggiNews.matcher(getURL("http://www.example.com/"));

                      } catch(UnknownHostException tt){
                             Toast t=Toast.makeText(MainActivity.this, "NESSUNA CONNESSIONE DISPONIBILE!\nATTIVA LA RETE PER QUESTO SERVIZIO.", Toast.LENGTH_LONG);
                             t.setGravity(Gravity.TOP, 0, 240);
                             t.show();
                         }catch (Exception e) {  
                                Toast t=Toast.makeText(MainActivity.this, "[ERRORE GENERICO!]\n"+e, Toast.LENGTH_LONG);
                                 t.setGravity(Gravity.TOP, 0, 240);
                                 t.show();
                } 

specifically for the getURL() method:

static final String getURL(String u)throws IOException {
    URL url = new URL(u);
     InputStream content = (InputStream) url.getContent();
      BufferedReader in = new BufferedReader(new InputStreamReader(content));
      String line;
      String a="";
        while ((line = in.readLine()) != null) {
             a+=line; 
             }
      in.close();
      content.close();   
   return a;
   }

The method getURL is always in the same class, how can I properly insert a ProgressDialog? Thank you.


回答1:


You can do it using different Thread for Network operations as follows

 ProgressDialog progress = ProgressDialog.show(this, "", "Loading ...", true);
 new Thread(new Runnable(){
       @Override
       public void run(){
          try{
                leggiNews = Pattern.compile("<p><a href='(.*?)' class='rossobig'>(.*?)</a><br/>(.*?)</p>"); 
                leggi = leggiNews.matcher(getURL("http://www.example.com/"));
          } catch(UnknownHostException tt){
                         tt.printStackTrace();
          } catch (Exception e) {  
                           e.printStackTrace();
            } 
          runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        progress.dismiss();
                    }
                });

      }
 }).start();

Please make sure that object of ProgressDialog will be class variable here



来源:https://stackoverflow.com/questions/15023616/android-progressdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!