Q:

android.os.NetworkOnMainThreadException - Web Socket

I am testing chatting service with "Web socket"
The code doesn't make any error. But, when I lunch the app, it even cannot be started with
the error 
06-10 16:59:22.420: E/AndroidRuntime(10814): FATAL EXCEPTION: main
06-10 16:59:22.420: E/AndroidRuntime(10814): java.lang.RuntimeException: Unable to resume activity {com.websocketchat_test/com.websocketchat_test.WebsocketChat}: android.os.NetworkOnMainThreadException
06-10 16:59:22.420: E/AndroidRuntime(10814):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2466)
06-10 16:59:22.420: E/AndroidRuntime(10814):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2494)
06-10 16:59:22.420: E/AndroidRuntime(10814):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2008)
06-10 16:59:22.420: E/AndroidRuntime(10814):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-10 16:59:22.420: E/AndroidRuntime(10814):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)

My main code is here..
public class WebsocketChat extends Activity implements View.OnClickListener{

private static String SERVER_HOST = "10.0.0.1";

private WebSocket websocket;
private EditText messageEditText;
private TextView logsTextView;

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    messageEditText = (EditText)this.findViewById(R.id.messageEditText);
    logsTextView = (TextView)this.findViewById(R.id.logsTextView);

    try {
        URI url  = new URI("ws://" + SERVER_HOST + "/");
        websocket = new WebSocketConnection(url);

        websocket.setEventHandler(new WebSocketEventHandler() {
            Handler handler = new Handler();

            public void onOpen() {
                updateLog("connected.");
            }

            public void onMessage(final WebSocketMessage message) {
                handler.post(new Runnable() {
                    public void run() {
                        updateLog(message.getText().trim());
                    }
                });
            }

            public void onClose() {
                updateLog("closed");
            }
        });

        View sendButton = this.findViewById(R.id.sendButton);
        sendButton.setOnClickListener(this);

    } catch (WebSocketException wse) {
        wse.printStackTrace();
    } catch (URISyntaxException use) {
        use.printStackTrace();
    }
} 

@Override
public void onResume() {
    super.onResume();

    try {
        websocket.connect();
    } catch (WebSocketException wse) {
        wse.printStackTrace();
    }
}

public void onClick(View view) {
    try {
        websocket.send(messageEditText.getText().toString());
        messageEditText.setText("");
    } catch (WebSocketException wse) {
        wse.printStackTrace();
    }
}

private void updateLog(String message) {
    logsTextView.setText("> " + message + "\n" + logsTextView.getText());
}

}
I don't know why it's happened. I added internet permission for sure..
Any suggestion would be OK. Reply it please.

A:

Recent Android version won't allow any network activity on your main/UI thread. You might have a look at AutobahnAndroid - an Android/Java WebSocket implementation that does all networking on background threads.
Disclosure: I am original author of Autobahn and work for Tavendo.

