2015-04-15 67 views
0

簡單代碼:Android的 - 嘗試發送POST請求,拋出一個異常,沒有消息

String URL = "http://www.google.com"; 
         try { 
          HttpClient httpClient = new DefaultHttpClient(); 
          HttpPost httpPost = new HttpPost("http://xocircle.freeiz.com/index.php?id=425231&data=joiragiiij99newooo"); 
          // Add your data 
          List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2); 
          httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList)); 
          // execute HTTP post request 
          HttpResponse response = httpClient.execute(httpPost); 
         } catch(Exception ex) { 
          ex.printStackTrace(); 
          Log.w("Error", ex.getMessage()); 
         } 

我在我的Android應用程序運行它,它崩潰的應用程序。 這裏是消息:

04-15 18:21:27.645 16150-16150/com.nimrod.xocircle W/Error﹕ Try to send 
04-15 18:21:27.765 16150-16150/com.nimrod.xocircle W/System.err﹕ android.os.NetworkOnMainThreadException 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 

04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1227) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.nimrod.xocircle.PlayBoard$1.onClick(PlayBoard.java:94) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.view.View.performClick(View.java:4630) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.view.View$PerformClick.run(View.java:19339) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Looper.loop(Looper.java:157) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5335) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
04-15 18:21:27.775 16150-16150/com.nimrod.xocircle W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 
04-15 18:21:27.775 16150-16150/com.nimrod.xocircle W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41fcdc08) 

但我不明白我。

我確實有權限,而且確實有互聯網連接。

任何幫助?

回答

1

你有一個android.os.NetworkOnMainThreadException,你需要執行一個線程內的所有網絡操作。所以只需將你的代碼包裝在一個線程或一個異步任務中,它就可以工作。

1

您可以使用

new Thread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 

     } 
    }).start(); 

,並把你的代碼run方法中。

相關問題