-1

I am making an Android application that gets text from a webpage and displays it. My application always crashes when I am trying to get the text from the page. This is my code:

HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://www.midstatexc.org/uploads/8/9/5/4/8954191/hampshire_2012_invitational.txt"); //URL!
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = null;
        while ((line = reader.readLine()) != null) {
            result += line + "\n";
            htmlText = result;
        }

I do have the permission for internet in my manifest. It crashes with this message:

04-09 10:07:30.517: E/AndroidRuntime(26588): FATAL EXCEPTION: main
04-09 10:07:30.517: E/AndroidRuntime(26588): java.lang.IllegalStateException: Could not execute method of the activity
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.view.View$1.onClick(View.java:3044)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.view.View.performClick(View.java:3511)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.view.View$PerformClick.run(View.java:14105)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.os.Handler.handleCallback(Handler.java:605)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.os.Looper.loop(Looper.java:137)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at dalvik.system.NativeStart.main(Native Method)
04-09 10:07:30.517: E/AndroidRuntime(26588): Caused by: java.lang.reflect.InvocationTargetException
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.view.View$1.onClick(View.java:3039)
04-09 10:07:30.517: E/AndroidRuntime(26588):    ... 11 more
04-09 10:07:30.517: E/AndroidRuntime(26588): Caused by: android.os.NetworkOnMainThreadException
04-09 10:07:30.517: E/AndroidRuntime(26588):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at com.CarbonDev.codecabana.MainMenu.getHTML(MainMenu.java:55)
04-09 10:07:30.517: E/AndroidRuntime(26588):    at com.CarbonDev.codecabana.MainMenu.compile(MainMenu.java:39)
04-09 10:07:30.517: E/AndroidRuntime(26588):    ... 14 more

Thanks. I'm a really new to Android, and I need all the help I can get.

Allison
  • 2,213
  • 4
  • 32
  • 56

1 Answers1

2

You must not do network operations in the gui thread do them instad in a seperart thread or in in an AscyTask.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
}
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Just to elaborate. The problem is this line in your exception: `04-09 10:07:30.517: E/AndroidRuntime(26588): Caused by: android.os.NetworkOnMainThreadException` – Darwind Apr 09 '13 at 16:17
  • I was interrupted due a phone call. However the other answer has a nice example. Well it's gone I copied an example from the documentation. – rekire Apr 09 '13 at 16:18