2013-03-09 99 views
-1

我有這種方法來啓動套接字連接,以便我可以在我的PC和手機之間聊天。我的電腦上已經有一臺等待與Android連接的服務器。Android套接字創建失敗

private void connectToServer() throws IOException 
    { 
     showMessage("Starting Connection..."); 
     connection = new Socket(InetAddress.getByName(serverIP), 0511); 
     showMessage("\n Connection Established: "+connection.getInetAddress().getHostName()); 
    } 

,這是我得到的錯誤,當我運行它:

03-08 19:02:36.847: E/AndroidRuntime(2524): FATAL EXCEPTION: main 
03-08 19:02:36.847: E/AndroidRuntime(2524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jister13.chattest/com.jister13.chattest.ChatRoom}: android.os.NetworkOnMainThreadException 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread.access$700(ActivityThread.java:139) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.os.Looper.loop(Looper.java:137) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread.main(ActivityThread.java:4918) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at dalvik.system.NativeStart.main(Native Method) 
03-08 19:02:36.847: E/AndroidRuntime(2524): Caused by: android.os.NetworkOnMainThreadException 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at libcore.io.IoBridge.connect(IoBridge.java:112) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.net.Socket.startupSocket(Socket.java:565) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at java.net.Socket.<init>(Socket.java:225) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at com.jister13.chattest.ChatRoom.connectToServer(ChatRoom.java:88) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at com.jister13.chattest.ChatRoom.startRunning(ChatRoom.java:74) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at com.jister13.chattest.ChatRoom.onCreate(ChatRoom.java:69) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.Activity.performCreate(Activity.java:5048) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052) 
03-08 19:02:36.847: E/AndroidRuntime(2524):  ... 11 more 

有誰知道這個問題可能是什麼?

+1

當您搜索該錯誤時,StackOverflow上數百個匹配問題中的任何一個沒有幫助? – 2013-03-09 00:07:43

回答

1

請閱讀包含在拋出的異常中的錯誤!

Caused by: android.os.NetworkOnMainThreadException

它拋出一個異常,因爲你試圖做的主UI線程上的網絡操作。在Android中,您必須從UI線程中執行耗時的任務(網絡,數據庫訪問,冗長的數字運算等)。

您需要在單獨的非UI線程上執行此操作。

+0

那麼我該怎麼做,因爲我不知道從這一點開始做什麼。 – Josh 2013-03-09 00:10:07

+0

閱讀關於在單獨線程上執行聯網功能的教程。一個AsyncTask教程可能是一個好的開始。說真的,沒有試圖聽到屁股,你需要在這裏幫助你多一點,做研究和學習。只有在你已經顯示出一些努力並且你仍然真的陷入困境時才詢問StackOverflow。 – Trevor 2013-03-09 00:15:28