• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

read from remote text file

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi .. i need a code to read a text file from an online source and display the content it in a TextView. the code should be placed in the onCreate method of the main activity of an android project. i tried a few codes but no result.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So show us what you tried and what you're stuck on.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So show us what you tried and what you're stuck on.



this is what i've tried :



but the app crashes on start and the error refers to this line:
 
Ranch Hand
Posts: 314
2
Android VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

You can do this as follows:

1. Place your text file in '/res/raw' to indicate that this file contains raw data.
2. Then read in a manner similar to the following:



Customize as needed.

Hope this helps.
Darryl
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl A. J. Staflund wrote:Hi there,

You can do this as follows:

1. Place your text file in '/res/raw' to indicate that this file contains raw data.
2. Then read in a manner similar to the following:


the file should be read from a URL because it gets updated from time to time.
 
Marshal
Posts: 4825
605
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohsen Dehghani wrote:the app crashes on start and the error refers to this line:


It looks like you are trying to do network operations on the main (UI) thread. Use LogCat - you should see something like: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork). For debugging, you can temporarily change the thread policy to allow network operations, but the real fix is to move this off to another thread.

 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
For debugging, you can temporarily change the thread policy to allow network operations, but the real fix is to move this off to another thread.



thanks for your precious advice. the app is working now but do you mean that i shouldn't publish it as is? if so, how can i "move this off to another thread"?
 
Ron McLeod
Marshal
Posts: 4825
605
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concern is that if your network operation takes some time (due to server overload, poor network connectivity, etc.), that your UI will become unresponsive and appear to be frozen.

The most common way to offload to another thread would be to extend the AsyncTask helper class. Do your work in the doInBackground method, and process the retrieved data in the onPostExecute method. If you are gonig to update the UI from the worker thread, you will need to use something like runOnUiThread.

Correction: AsyncTask takes care of running the onPostCreate method in the main/UI thread for you - so no need to so anything special.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
Correction: AsyncTask takes care of running the onPostCreate method in the main/UI thread for you - so no need to so anything special.



thanks a lot for your edit!
I've tried to make an asynctask to handle the desired operation and added it to the MainActivity but i guess it's a bit complicated for me. can you trim it in a way that makes sense?? and how can i call it in the onCreate block?
 
Ron McLeod
Marshal
Posts: 4825
605
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are pretty close - just clean-up a few things and you'll be done.

Remove the URL from the ReadTextTask class and pass it in as an argument in case you want this to be configurable or come from a database later-on. Also, to keep things simple (for now), it might be better just to return the value from the server as a String, especially if all you are going to do with it is display it in the UI. Finally, since you aren't using onProgressUpdate, just remove it.

That would leave you with:


Now to call it form your onCreate method, use your existing code to create the URL, create a new instance of your class, and then execute it with the URL as the argument.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:You are pretty close - just clean-up a few things and you'll be done.



thanks a lot! it seems to work fine unless it appears to return an empty string! this is my code so far:
 
Ron McLeod
Marshal
Posts: 4825
605
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that the UI is being updated with the value of the instance variable (empty String) rather than what was read from the server.

Delete the line with private String str =""; at the top of your MainActivity class, and add String str = null; before the try block in your ReadTextTask class.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:The problem is that the UI is being updated with the value of the instance variable (empty String) rather than what was read from the server.

Delete the line with private String str =""; at the top of your MainActivity class, and add String str = null; before the try block in your ReadTextTask class.



thanks a lot. works like charm
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it this file only has the one line in it.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I take it this file only has the one line in it.


only a number
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic