Android Socket Client自行关闭

时间:2012-12-28 21:19:56

标签: android sockets client logcat

  

可能重复:
  android.os.NetworkOnMainThreadException
  Android Socket Client didn’t send and closes itself

我是java和android编程的新手,我正在尝试为我的电脑编程Android客户端和服务器(Windows 7)。有了这个连接,我想从PC发送一个字符串到Android应用程序,也从应用程序发送到电脑。我检查了我的服务器,它是否正确编程,确实如此。所以我必须在我的客户端出错。当我启动应用程序时。该应用程序启动,但当我想从我的应用程序发送一个字符串到电脑时,应用程序只是关闭自己。现在我确实需要你的帮助。我已经在应用程序上编程超过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

1 个答案:

答案 0 :(得分: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