Possible Duplicate:
android.os.NetworkOnMainThreadException
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:
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);
I've also tried something very similar to this: How to read text file in android from web? (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.