0

Good afternoon all,

I'm having trouble reading the contents of a .txt file on my web server.

Below is my code that i have so far and i do have the internet permission in my manifest. i'm not sure what is wrong. i've tried following a lot of tutorials on how to read the contents of a .txt file on the web but none have been successful so far.

Code: songlistURL = "http://some.com/website/file.txt"; URL u = null;

try {
u = new URL(songlistURL);
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);
bo.write(buffer);
textFileStr = bo.toString();

   } catch (MalformedURLException e) {
       e.printStackTrace();
   } catch (ProtocolException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }

Log.d("text file", "" + textFileStr);

I'm really tired so I may just be brain farting something, but I don't get the logcat error output either:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stillbent.staybent/com.stillbent.staybent.Samples}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at com.stillbent.staybent.Samples.onCreate(Samples.java:61)
at android.app.Activity.performCreate(Activity.java:5058)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
... 11 more

Would anyone be able to point me in the right direction possibly?

Thanks in advance, Nick

Nick Willcox
  • 59
  • 1
  • 1
  • 7
  • Also the code should capture the number of bytes read by by the read() method and use it for the write(). – NormR Oct 05 '13 at 23:49

1 Answers1

1

You are getting a 'NetworkOnMainThreadException'.

You are not supposed to do any blocking operations on the main thread - this would block the processing of the UI.

You should put your networking code into an AsyncTask.

Tom
  • 17,103
  • 8
  • 67
  • 75
  • Thanks for the direction. I'm sorry I missed that in the logcat. I created a class file called "TextFromWeb.java" and moved all the above code to my new class file. To call it, i use: new TextFromWeb().execute(txt); and then to verify: Log.d("txt", "" + txt); And it seems to be pulling down the data expected. Thank you for the direction. – Nick Willcox Oct 05 '13 at 23:46