Android ProgressDialog
Asked Answered
D

1

0

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.

Dunois answered 22/2, 2013 at 11:52 Comment(4)
It seems to me that you are doing some networking? am I right? If this is the case please take a look at AsyncTask.Melchior
Yes, but the AsyncTask class must be external or internal?Dunois
That is basically up to you. You can make a class which extends AsyncTask or you can make a interal private class which extends AsynctTask. Its just a matter of what suits you the best.Melchior
I found this sample code pastebin.com/UEhdPKDq but in my case where I should put the getURL() method in the private class MyNewTask ?Dunois
P
0

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

Peck answered 22/2, 2013 at 12:9 Comment(1)
With this code the application crashes!I declared ProgressDialog like this: final ProgressDialog progress; progress = ProgressDialog.show(this, "", "Loading ...", true);Dunois

© 2022 - 2024 — McMap. All rights reserved.