Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Text File From Server on Android

Tags:

file

android

http

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.

like image 817
Chris Avatar asked May 27 '10 14:05

Chris


People also ask

How do I read a file on a server?

A text file on a server can be read with Javascript by downloading the file with Fetch / XHR and parsing the server response as text. Note that the file needs be on the same domain. If the file is on a different domain, then proper CORS response headers must be present.

How do I share a text file on Android?

setType("*/txt"); sharingIntent. putExtra(Intent. EXTRA_STREAM, file); startActivity(Intent. createChooser(sharingIntent, "share file with"));


1 Answers

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.

like image 73
aioobe Avatar answered Sep 20 '22 04:09

aioobe