2013-04-09 85 views
-1

我正在製作一個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非常陌生,我需要獲得所有的幫助。

+0

使用asyctask避免networkonmaainthreadexception – Raghunandan 2013-04-09 16:13:30

+0

http://stackoverflow.com/questions/15653739/network-on-main-thread-exception/15654018#15654018。看看鏈接 – Raghunandan 2013-04-09 16:16:19

回答

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"); 
    } 
} 
+0

已經有我的+1,但小例子不能傷害 – stealthjong 2013-04-09 16:15:47

+0

只是爲了闡述。問題是你的例外中的這一行: '04-09 10:07:30.517:E/AndroidRuntime(26588):引起:android.os.NetworkOnMainThreadException' – Darwind 2013-04-09 16:17:00

+0

我因電話中斷。但其他答案有一個很好的例子。那麼它已經不存在了,我從文檔中複製了一個例子。 – rekire 2013-04-09 16:18:13