title: Android: loading data from the web in background thread with a "progress" GUI?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog">Download a file with Android, and showing the progress in a ProgressDialog</a>  </p>
  </blockquote>
  
  
  
  <p>I want to load info from a web server into my app. Currently I'm doing it in the main thread, which I've read is very bad practice (if the request takes longer than 5 secs, the app crashes). </p>
  
  <p>So instead I'd like to learn how to move this operation to a background thread. Does this involve a service somehow? </p>
  
  <p>Here is a sample of the code where I make server requests: </p>
  
  <pre><code>        // send data
          URL url = new URL("http://www.myscript.php");
          URLConnection conn = url.openConnection(); 
          conn.setDoOutput(true); 
          OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
          wr.write(data);
          wr.flush();
  
          // Get the response
          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          StringBuilder sb = new StringBuilder(); 
          String line;
          while ((line = rd.readLine()) != null) {
              sb.append(line + "\n"); 
          }
  
          wr.close();
          rd.close();
  
          String result = sb.toString(); 
  
          Intent i = new Intent(searchEventActivity.this, searchResultsActivity.class);
              i.putExtra("result", result);
          startActivity(i);
  </code></pre>
  
  <p>So I'm waiting to build up a JSON string response, then I'm passing that string to a new activity. This is a timely operation, and instead of hanging the UI, I'd like to show the user a nice "Progress" bar of some sort (even one of those circles with the spinning lights is good) while this URL business is happening in a background thread. </p>
  
  <p>Thanks for any help or links to tutorials. </p>