-1

我正在制作一个从网页获取文本并显示它的 Android 应用程序。当我试图从页面中获取文本时,我的应用程序总是崩溃。这是我的代码:

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;
        }

我的清单中确实有互联网许可。它与此消息一起崩溃:

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

谢谢。我是一个真正的 Android 新手,我需要我能得到的所有帮助。

4

1 回答 1

2

您不能在 gui 线程中执行网络操作,而是在 seperart 线程或 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");
     }
}
于 2013-04-09T16:13:35.303 回答