你好我試圖將數據發送到一個web服務Android的發送數據發佈到web服務
字符串URL =「https://www.myurl.com」;
URL urlobj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) urlobj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String urlParameters = GenerateUrlParameters(data);
con.setDoOutput(true);
con.setDoInput(true);
Log.i("test", "hit1");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
Log.i("test", "hit2");
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
Hit1被擊中,但它在下一行失敗並且不會命中hit2。我究竟做錯了什麼 ?
我logcat的
10-18 09:17:33.302: D/gralloc_goldfish(2228): Emulator without GPU emulation detected.
10-18 09:17:33.322: W/TextLayoutCache(2228): computeValuesWithHarfbuzz -- need to force to single run
10-18 09:17:37.862: I/test(2228): hit1
10-18 09:17:37.872: D/AndroidRuntime(2228): Shutting down VM
10-18 09:17:37.872: W/dalvikvm(2228): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
10-18 09:17:37.912: E/AndroidRuntime(2228): FATAL EXCEPTION: main
10-18 09:17:37.912: E/AndroidRuntime(2228): java.lang.IllegalStateException: Could not execute method of the activity
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.view.View$1.onClick(View.java:3039)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.view.View.performClick(View.java:3480)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.view.View$PerformClick.run(View.java:13983)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.os.Handler.handleCallback(Handler.java:605)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.os.Looper.loop(Looper.java:137)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 09:17:37.912: E/AndroidRuntime(2228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-18 09:17:37.912: E/AndroidRuntime(2228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-18 09:17:37.912: E/AndroidRuntime(2228): at dalvik.system.NativeStart.main(Native Method)
10-18 09:17:37.912: E/AndroidRuntime(2228): Caused by: java.lang.reflect.InvocationTargetException
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.view.View$1.onClick(View.java:3034)
10-18 09:17:37.912: E/AndroidRuntime(2228): ... 11 more
10-18 09:17:37.912: E/AndroidRuntime(2228): Caused by: android.os.NetworkOnMainThreadException
10-18 09:17:37.912: E/AndroidRuntime(2228): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-18 09:17:37.912: E/AndroidRuntime(2228): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
10-18 09:17:37.912: E/AndroidRuntime(2228): at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
10-18 09:17:37.912: E/AndroidRuntime(2228): at dk.example.epaypayment.EpayHttpRequest.MakePayment(EpayHttpRequest.java:53)
10-18 09:17:37.912: E/AndroidRuntime(2228): at dk.example.epaypayment.MainActivity.sendMessage(MainActivity.java:37)
10-18 09:17:37.912: E/AndroidRuntime(2228): ... 14 more
10-18 09:22:38.232: I/Process(2228): Sending signal. PID: 2228 SIG: 9
您可以發佈您的LogCat嗎? –
Logcat發佈... – Millerbean
請谷歌,異常android.os.NetworkOnMainThreadException –