-1

可能重复:
android.os.NetworkOnMainThreadException
Android Socket Client 未发送并自行关闭

我是java和android编程的新手,我正在尝试为我的电脑(windows 7)编写一个Android客户端和一个服务器。通过这个连接,我想将一个字符串从 pc 发送到 android 应用程序,也从应用程序发送到 pc。我检查了我的服务器,它是否编程正确并且是正确的。所以我的客户一定有一个错误。当我启动应用程序时。该应用程序启动,但是当我想从我的应用程序向电脑发送一个字符串时,该应用程序会自行关闭。现在我确实需要你的帮助。我已经在应用程序上编程超过 2 天了。

这是我的 LogCat,我真的希望你能告诉我在哪里可以找到我的错误。

12-28 21:10:29.348: I/dalvikvm(565): threadid=3: reacting to signal 3

12-28 21:10:29.428: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'

12-28 21:10:29.818: I/dalvikvm(565): threadid=3: reacting to signal 3

12-28 21:10:29.888: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'

12-28 21:10:30.258: D/gralloc_goldfish(565): Emulator without GPU emulation detected.

12-28 21:10:30.319: I/dalvikvm(565): threadid=3: reacting to signal 3

12-28 21:10:30.348: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'

12-28 21:11:12.498: D/AndroidRuntime(565): Shutting down VM

12-28 21:11:12.508: W/dalvikvm(565): threadid=1: thread exiting with uncaught exception 
(group=0x409c01f8)

12-28 21:11:12.540: E/AndroidRuntime(565): FATAL EXCEPTION: main

12-28 21:11:12.540: E/AndroidRuntime(565): android.os.NetworkOnMainThreadException

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
libcore.io.IoBridge.connectErrno(IoBridge.java:127)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
libcore.io.IoBridge.connect(IoBridge.java:112)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.net.Socket.startupSocket(Socket.java:566)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.net.Socket.tryAllAddresses(Socket.java:127)

12-28 21:11:12.540: E/AndroidRuntime(565):  at java.net.Socket.<init>(Socket.java:177)

12-28 21:11:12.540: E/AndroidRuntime(565):  at java.net.Socket.<init>(Socket.java:149)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
net.ibasic.AndroidClient$1.onClick(AndroidClient.java:50)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.view.View.performClick(View.java:3511)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.view.View$PerformClick.run(View.java:14105)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.os.Handler.handleCallback(Handler.java:605)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.os.Handler.dispatchMessage(Handler.java:92)

12-28 21:11:12.540: E/AndroidRuntime(565):  at android.os.Looper.loop(Looper.java:137)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
android.app.ActivityThread.main(ActivityThread.java:4424)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.lang.reflect.Method.invokeNative(Native Method)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
java.lang.reflect.Method.invoke(Method.java:511)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

12-28 21:11:12.540: E/AndroidRuntime(565):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

12-28 21:11:12.540: E/AndroidRuntime(565):  at dalvik.system.NativeStart.main(Native 
Method)

12-28 21:11:13.178: I/dalvikvm(565): threadid=3: reacting to signal 3

12-28 21:11:13.218: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'

12-28 21:11:15.288: I/Process(565): Sending signal. PID: 565 SIG: 9
4

1 回答 1

1

听起来您可能正在主 UI 中而不是像 AsyncTask 这样的背景中做网络的事情 这是一个如何设置的基本示例

public class MyNetworkTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);

}

@Override
protected String doInBackground(String... params) {
//do your work here
    return something;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
       // do something with data here-display it or send to mainactivity

}

这是它的文档链接:http: //developer.android.com/reference/android/os/AsyncTask.html

于 2012-12-28T21:31:27.290 回答