title: Read text file from server in Android app
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception">android.os.NetworkOnMainThreadException</a>  </p>
  </blockquote>
  
  
  
  <p>I want to open a text file (one line with a few words) from a server in a TextView, but my current code crashes the app when I open the activity. I've tried: </p>
  
  <pre><code>urlTextOut = (TextView) findViewById(R.id.URLtextView); 
  StringBuilder text = new StringBuilder();
  
  try {
      String str = "";
      URL url = new URL("http://mysite.com/text1.txt");
  
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  
      while ((str = in.readLine()) != null) {
           text.append(str);
      }
      in.close();
  } catch (MalformedURLException e1) {
  } catch (IOException e) {
  }
  
  urlTextOut.setText(text);
  </code></pre>
  
  <p>I've also tried something very similar to this: <a href="https://stackoverflow.com/questions/6099429/how-to-read-text-file-in-android-from-web">How to read text file in android from web?</a> (with added try/catches where it wanted them) but that also crashed the app. I've tried looking at Apache's HttpComponents but couldn't figure out what I was meant to do with the downloaded zip file. </p>