41

I have a text file on my server. I want to open the text file from my Android App and then display the text in a TextView. I cannot find any examples of how to do a basic connection to a server and feed the data into a String.

Any help you can provide would be appreciated.

Chris
  • 5,485
  • 15
  • 68
  • 130
  • How do you serve the text-file on the server? Through HTTP? – aioobe May 27 '10 at 15:00
  • Yeah, if I go to http://www.mysite.com/thefile.txt, I can see the text. On the iPhone I am able to connect to the file and feed it into a String, then do whatever I want with it. – Chris May 27 '10 at 15:04

3 Answers3

65

Try the following:

try {
    // Create a URL for the desired page
    URL url = new URL("mysite.com/thefile.txt");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

(taken from Exampledepot: Getting text from URL)

Should work well on Android.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 3
    Note: Always close your `Closeables` in the `finally` block as this is the only way to assure that they get closed even when an exception gets thrown. – Octavian Helm Jan 14 '11 at 19:45
15

While URL.openStream will work, you would be better off using the Apache HttpClient library that comes with Android for HTTP. Among other reasons, you can use content encoding (gzip) with it, and that will make text file transfers much smaller (better battery life, less net usage) and faster.

There are various ways to use HttpClient, and several helpers exist to wrap things and make it easier. See this post for more details on that: Android project using httpclient --> http.client (apache), post/get method (and note the HttpHelper I included there does use gzip, though not all do).

Also, regardless of what method you use to retrieve the data over HTTP, you'll want to use AysncTask (or Handler) to make sure not to block the UI thread while making the network call.

And note that you should pretty much NEVER just use URL.openStream (without setting some configuration, like timeouts), though many examples show that, because it will block indefinitely if you the server is unavailable (by default, it has no timeout): URL.openStream() Might Leave You Hanging.

Community
  • 1
  • 1
Charlie Collins
  • 8,806
  • 4
  • 32
  • 41
  • 1
    This is a very old answser, the Android team now recommends using the standard java.net stuff. For a really nice wrapper around it (that handles the timeouts and a LOT more) check out Kevin Sawicki's HttpRequest: https://github.com/kevinsawicki/http-request – Charlie Collins Jan 08 '13 at 14:50
  • The accepted answer here above is good, but I'd still be very wary of the timeouts. In that example they aren't being set and url.openStream back in the day used to just hang when the server wasn't responsive (default timeout is/was 0, which means don't time out ever). The article I linked is gone, but there are plenty of others that note the same: https://eventuallyconsistent.net/2011/08/02/working-with-urlconnection-and-timeouts/ – Charlie Collins Mar 05 '18 at 20:13
7

Don't forget to add internet permissions to the manifest when taking net resources: (add in manifest).

baron_bartek
  • 1,073
  • 2
  • 20
  • 39