我正在製作一個Android應用程序,它從網頁獲取文本並顯示它。當我試圖從頁面獲取文本時,我的應用程序總是崩潰。這是我的代碼:Android上的HTTP請求崩潰
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非常陌生,我需要獲得所有的幫助。
使用asyctask避免networkonmaainthreadexception – Raghunandan 2013-04-09 16:13:30
http://stackoverflow.com/questions/15653739/network-on-main-thread-exception/15654018#15654018。看看鏈接 – Raghunandan 2013-04-09 16:16:19