Reading Text File From Server on Android
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.
Android - How can I read a text file from a url?
Try using an HTTPUrlConnection or a OKHTTP Request to get the info, here try this:
Always do any kind of networking in a background thread else android will throw a NetworkOnMainThread Exception
new Thread(new Runnable(){
public void run(){
ArrayList<String> urls=new ArrayList<String>(); //to read each line
//TextView t; //to show the result, please declare and find it inside onCreate()
try {
// Create a URL for the desired page
URL url = new URL("http://somevaliddomain.com/somevalidfile"); //My text file location
//First open the connection
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(60000); // timing out in a minute
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//t=(TextView)findViewById(R.id.TextView1); // ideally do this in onCreate()
String str;
while ((str = in.readLine()) != null) {
urls.add(str);
}
in.close();
} catch (Exception e) {
Log.d("MyTag",e.toString());
}
//since we are in background thread, to post results we have to go back to ui thread. do the following for that
Activity.this.runOnUiThread(new Runnable(){
public void run(){
t.setText(urls.get(0)); // My TextFile has 3 lines
}
});
}
}).start();
Android read text file from server
Try this
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://lehakoeevents.co.za/dijo/weekly_content/stories/sway_rustenburg_22_08_15.txt");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
Log.i("HI", "" + total.toString());
Hope this helps!
Android read text file from internet
- Did you added internet permission to your Manifest file?
- Are you launching your code in separate thread (please don't catch NetworkOnMainThreadException)
- Check LogCat what exception do you have?
- Removed
c.setDoOutput(true);
this is used to send data to server.
Here how it should be:
new Thread() {
@Override
public void run() {
String path ="http://host.com/info.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView text = (TextView) findViewById(R.id.TextView1);
text.setText(bo.toString());
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
Read a text file from web in Android
Did you check the HttpResponce code you got from the http request to be 200?
Fetch Data from text file in a server to an Android app
You can use this code
new AsyncTask<String, Void, String>()
{
@Override
protected String doInBackground(String[] params) {
try
{
String line;
URL url = new URL("http://nomediakings.org/everyoneinsilico.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str="";
while ((line = in.readLine()) != null) {
str+=line;
}
in.close();
}catch (Exception ex)
{
ex.getMessage();
}
return null;
}
}.execute("");
Trying to read a text file from my web server, getting a file not found error
Start from android 9 clear text http traffic is not permitted so any device having latest android os will fail to communicate using http. so use https and update your url as:
String TextFileURL = "https://leevalleyboats.co.uk/textfiles/videos.txt";
I edited back my answer to the previous solution as you mentioned in comment that worked for you.
Related Topics
Usb Device Access Pop-Up Suppression
Access the Http Response Headers in a Webview
Trigger Mediascanner on Specific Path (Folder), How To
Install an APK File from Command Prompt
Android - How to Download a File from a Webserver
Emulator Error: This Avd's Configuration Is Missing a Kernel File
How to Securely Store Credentials (Password) in Android Application
Android Studio Isn't Opening on Windows
Android What Does the Cliptopadding Attribute Do
How to Download the Android Sdk Without Downloading Android Studio
Get the Current Fragment Object
How to Disable Past Dates in Android Date Picker
Could Not Find Com.Android.Tools.Build:Aapt2:3.2.0
Use Uri Builder in Android or Create Url with Variables
How to Determine If Network Type Is 2G, 3G or 4G