2

i found the following code which is supposed to let me read the directory contents of from a web host (my website in this case)

String urlStr = "http://2112design.com/tabs/drum_tabs";
URL url;
try {
    url = new URL(urlStr);
    java.net.URLConnection con = url.openConnection();
    con.connect();
    java.io.BufferedReader in =
      new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
    String line;
    for (; (line = in.readLine()) != null; ) {
        Log.d("MainActivity", "read from web "+line);
    }
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

when i run this, con.connect(); fails with the error trace like this:

android.os.NetworkOnMainThreadException
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
java.net.InetAddress.lookupHostByName(InetAddress.java:385)

it looks like there is some kind of permission problem StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) but when i use a web browser on the same device and go to that URL, it opens fine.

i'm not very good on the webby stuff, can someone please help, thanks

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
steveh
  • 1,352
  • 2
  • 27
  • 41

3 Answers3

5

you should not do network requests on main thread , try to use AsyncTask, or start a new thread, for example.

     (new Thread(new Runnable() {

                                        @Override
                                        public void run() {
                                            try {
                                                url = new URL(urlStr);
                                                java.net.URLConnection con = url.openConnection();
                                                con.connect();
                                                java.io.BufferedReader in =
                                                  new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
                                                String line;
                                                for (; (line = in.readLine()) != null; ) {
                                                    Log.d("MainActivity", "read from web "+line);
                                                }
                                            } catch (MalformedURLException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                            } catch (IOException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                            }

                                        }
                                    })).start();

Or you can use

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = 
             new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

check this question for more information.

Community
  • 1
  • 1
Aram
  • 695
  • 1
  • 5
  • 20
4

This is because you are trying to access network from the main thread which is not allowed in Android

You can either use AsyncTask or create another thread and perform the operation in it

Please feel free to ask if you have any doubts regarding how to implement using either AsyncTask or thread. Do let me know if you want the code.

pvn
  • 2,016
  • 19
  • 33
  • great, thanks, i do know about asymc task so i'll try that now – steveh Jan 24 '13 at 02:39
  • so i sounded that code with new Thread(new Runnable() { public void run() { CODE } }).start() which i'm sure helped but now i get an error after lookHostByName java.lang.SecurityException: Permission denied (missing INTERNET permission?). i checked again and i can open that URL with a browser. thx – steveh Jan 24 '13 at 02:48
  • i tried a simillar thing by making a new class public class DownloadFilesTask extends AsyncTask and in doInBackground try the same code and same problem. any ideas? thanks – steveh Jan 24 '13 at 03:00
  • i just added to my manifest and all is ok now. thanks to everyone for helping. – steveh Jan 24 '13 at 03:07
2

You are executing the Network Operations in UI Thread. That is why you are getting NetworkOnMainThreadException.

Change your code into an AsyncTask or else to a Thread which will solve your issue.

TNR
  • 5,839
  • 3
  • 33
  • 62