-1

Possible Duplicate:
android.os.NetworkOnMainThreadException
Android Socket Client didn’t send and closes itself

i am new to java and android programming and i am trying to programm an Android Client and a server for my pc (windows 7). With this connection i want to send a string from pc to the android app and also from the app to the pc. I checked my server, whether it is programmed correctly and it is. So i must habe an error in my client. When i launch the app. The app starts but when i want to send a string from my app to the pc the app just closes itself. Now i need your help indeed. I have been programming just on the app for more than 2 days.

Here is my LogCat and i really hope that you can tell me where i can find my mistake.

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
Community
  • 1
  • 1
Lukas5060
  • 47
  • 1
  • 9
  • Given this exception in the stack trace `android.os.NetworkOnMainThreadException` I am expecting that you're doing Network activity in the main thread, which Android doesn't seem to like. Do a search for the exception and you're bound to find legions of people who have made the same mistake and how they fixed it. – Frank van Puffelen Dec 28 '12 at 21:26
  • Ok. Thank you. I will look up for this. – Lukas5060 Dec 28 '12 at 21:28

1 Answers1

1

It sounds like you probably are doing the network stuff in main UI instead of background like AsyncTask Here is a basic example of how it is set up

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

}

Here is a link to the documentation for it: http://developer.android.com/reference/android/os/AsyncTask.html

codeMagic
  • 44,549
  • 13
  • 77
  • 93