我正在创建一个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的新手,我需要得到所有帮助。
答案 0 :(得分: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");
}
}