unable connect to server from application on devic

2019-03-05 13:29发布

问题:

I have problem with testing my application on device.
I already successfully open the localhost in browser from my device, but when i enter my ip to connect my application with web service it didn't work.
I always get the "unfortunately, [my apps name] has stopped" message.
Anyone who knows about this problem, please help me. I really need help to solve this. thank you very much

logcat - i get it from usb debug on device

06-10 11:31:15.551: D/dalvikvm(14590): Late-enabling CheckJNI
06-10 11:31:15.809: D/TextLayoutCache(14590): Using debug level: 0 - Debug Enabled: 0
06-10 11:31:15.902: D/libEGL(14590): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
06-10 11:31:15.910: D/libEGL(14590): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
06-10 11:31:15.910: D/libEGL(14590): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
06-10 11:31:16.160: D/OpenGLRenderer(14590): Enabling debug mode 0
06-10 11:31:19.770: D/OpenGLRenderer(14590): Flushing caches (mode 0)
06-10 11:31:22.309: D/OpenGLRenderer(14590): Flushing caches (mode 0)
06-10 11:31:30.848: D/TryLoginCheck(14590): Here
06-10 11:31:30.848: I/System.out(14590): UserNametes
06-10 11:31:30.848: I/System.out(14590): passwordTes12345*
06-10 11:31:30.848: D/Parameters(14590): username=tes&password=Tes12345*
06-10 11:31:30.863: D/AndroidRuntime(14590): Shutting down VM
06-10 11:31:30.863: W/dalvikvm(14590): threadid=1: thread exiting with uncaught exception (group=0x40bd31f8)
06-10 11:31:30.863: E/AndroidRuntime(14590): FATAL EXCEPTION: main
06-10 11:31:30.863: E/AndroidRuntime(14590): android.os.NetworkOnMainThreadException
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.io.IoBridge.connect(IoBridge.java:112)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at java.net.Socket.connect(Socket.java:842)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at com.karismaelearning.Login.tryLogin(Login.java:128)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at com.karismaelearning.Login$3.onClick(Login.java:73)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.view.View.performClick(View.java:3558)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.view.View$PerformClick.run(View.java:14157)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.os.Handler.handleCallback(Handler.java:605)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.os.Looper.loop(Looper.java:137)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at android.app.ActivityThread.main(ActivityThread.java:4514)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at java.lang.reflect.Method.invoke(Method.java:511)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-10 11:31:30.863: E/AndroidRuntime(14590):    at dalvik.system.NativeStart.main(Native Method)
06-10 11:31:30.918: D/dalvikvm(14590): GC_CONCURRENT freed 237K, 6% free 6863K/7239K, paused 2ms+2ms

回答1:

you are getting android.os.NetworkOnMainThreadException :

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

do your code this way in side AsyncTask