简单代码:
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)
但我无法理解我。
我有权限,我确实有互联网连接。
任何帮助?
答案 0 :(得分:1)
你有一个android.os.NetworkOnMainThreadException,你需要在一个线程内执行所有网络操作。因此,只需将代码包装在线程或异步任务中,它就可以正常工作。
答案 1 :(得分:1)
您可以使用
new Thread(new Runnable()
{
@Override
public void run()
{
}
}).start();
并将您的代码放在run
方法中。