Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:53:00+00:00 2026-06-02T11:53:00+00:00

I tried to use as a Android client Websocket – weberknecht with a thread

  • 0

I tried to use as a Android client Websocket – weberknecht with a thread to send every second a message to a Nodejs Websocket (js): Node WebSocket Server

It work fine on android 2 or 2.3.3, but when i trie on android 4 i get an error on websocket.connect();

Do you know why or what i am doing wrong?

My code is:

public class MainThread extends Thread {

    private boolean is_running;
    private URI url = null;
    private de.roderick.weberknecht.WebSocket websocket = null;

    public MainThread() {
        this.is_running = false;
        try {
            url = new URI("ws://192.168.1.88:8088/");
            websocket = new WebSocketConnection(url);

            websocket.setEventHandler(new WebSocketEventHandler() {
                public void onOpen(){
                    System.out.println("--open");
                }
                public void onMessage(WebSocketMessage message){
                    System.out.println("--received message: " + message.getText());
                }
                public void onClose(){
                    System.out.println("--close");
                }
            });
        } catch (WebSocketException wse) {
            wse.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (URISyntaxException use) {
            use.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
    @Override
    public void run() {
        while(is_running){
            try {
                websocket.send("hello world");
                Thread.sleep(10000);
            }
            catch (WebSocketException wse) {
                wse.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();  
            }

        }
    }

    public void set_running(boolean is_running) {
        if(is_running == true)
            try {
                websocket.connect();
                this.is_running = is_running;
            } catch (WebSocketException e) {
                System.out.println(e.toString());
                //e.printStackTrace();  
            }
    }
}

and the error I get on android 4:

ERROR/AndroidRuntime(562): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biskis.test.websocket/com.biskis.test.websocket.GameActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
        at libcore.io.IoBridge.connect(IoBridge.java:112)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.Socket.startupSocket(Socket.java:566)
        at java.net.Socket.tryAllAddresses(Socket.java:127)
        at java.net.Socket.<init>(Socket.java:177)
        at java.net.Socket.<init>(Socket.java:149)
        at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:238)
        at de.roderick.weberknecht.WebSocketConnection.connect(WebSocketConnection.java:83)
        at com.biskis.test.websocket.MainThread.set_running(MainThread.java:76)
        at com.biskis.test.websocket.GameActivity.onCreate(GameActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
        ... 11 more

Or do you know how to implement this in a better way to comunicate a client (java, android) to a server (nodejs, websocket) ?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:53:05+00:00Added an answer on June 2, 2026 at 11:53 am

    Don’t connect to the Websocket on the main Thread (i.e. in GameActivity.onCreate) and the error will go away. You could use an AsyncTask to connect to the Websocket in the background.

    Blocking the main thread by I/O operations (and network access is ultimately a I/O operation) will lead (in the worst case) to a sluggish user interface or Android killing your application because it didn’t react timely. See the designing for responsiveness document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a android webservice client application. I am trying to use the java
I would like to write a real time counter. I tried to use thread,
I tried to use Proguard in my Android project, i setup Proguard using command
I made a mail client with the use of javamail api for android and
Has someone tried to use MessagePack with an Android app? Is it possible? I
I'm developing an application for Android, and have tried to use ORMLite, but the
I tried to use SQLite on Android. I want to see my database file.
I tried to use an image file in my HTML template. But when I
I tried to use object expression to extend the IDelegateEvent, but in fsi there
I tried to use $.cssRule() plugin and found that it makes it inconvenient to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.