1

I am new to Android and this is my first app. I would like to know how to read a log file from a remote machine. I tried the following using IP address but that did not seem to work:

try
{
    URL url = new URL("`http://10.0.161.72/C:/adsb.log`");
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))
    //...
}

The file can also be accessible via a network drive. I tried the following code and it worked in Java but not android.

try
{
    File file = new File("T:\\AN\\3.00\\adsb.log");
    BufferedReader br = new BufferedReader(new FileReader(file));
}

If I copy the log file and place it in the assets folder of my android project, I am able to read the file. But I want to access it from the network drive or remote PC as the log file will eventually be updated every few seconds and I have to read it continuously.

Steve Ruble
  • 3,875
  • 21
  • 27
Aparna
  • 11
  • 2

1 Answers1

0

Please make sure you have given internet permission to your app.In your AndroidManifest.xml use this

<uses-permission android:name="android.permission.INTERNET"/>

coming to the log/file fetching from url, you should keep this in mind that the android system protects long blocks on using the Internet during the UI(User Interface) thread to prevent apps from becoming unresponsive. To properly download a file on the main thread you'll want to use some sort of Asynchronous thread to download the file and do a callback to the main thread to process it.I would suggest you to use AsynTask.

However for a quick fix, you can add this piece of code inside onCreate()

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

From the documents

using public StrictMode.ThreadPolicy.Builder permitAll() disables the detection of everything.Which means on UI thread network operations can be made now.While this may work, it is poor practice to run network calls on the UI thread.

Hope this helps.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
  • Thanks for the response. I will see to it that I eventually use AsyncTask. For now, I added the quick fix to the onCreate() and I already did give internet permissions but still unable to read the file. – Aparna Sep 03 '14 at 21:21
  • Can you please try refering [this answer](http://stackoverflow.com/questions/2922210/reading-text-file-from-server-on-android) and also [this tutorial](http://android-er.blogspot.in/2011/04/read-text-file-from-internet-using-java.html) to see if you can find where you are going wrong. – Sash_KP Sep 03 '14 at 21:42