0

如何显示此代码的 ProgressDialog?

 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();
                } 

专门针对 getURL() 方法:

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;
   }

getURL 方法始终在同一个类中,如何正确插入 ProgressDialog?谢谢你。

4

1 回答 1

0

您可以使用不同的 Thread 进行网络操作,如下所示

 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();

请确保 ProgressDialog 的对象在这里是类变量

于 2013-02-22T12:09:32.510 回答