user7179690
user7179690

Reputation: 1123

android.os.NetworkOnMainThreadException but my class extends Thread

I have a client class that extends Thread to start socket programming
my class code

class MyClientMessages extends Thread {
        Socket socket;
        int PORT = 5002;
        DataInputStream din;
        DataOutputStream dout;
        public MyClientMessages(String IP) {
            try {
                System.out.println("IP = ======= " + IP + " TYPE = " + TYPE);
                //*********** crash here ***************
                socket = new Socket(IP,PORT); // *********** it crash here *************
                din = new DataInputStream(socket.getInputStream());
                dout = new DataOutputStream(socket.getOutputStream());
                this.start();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            while (true) {
                byte[] data = new byte[1024];
                int size = 0;
                try {
                    while ((size = din.read(data)) > 0) {
                        final String str = new String(data,"UTF8");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                TextView textView = new TextView(ServerChat.this);
                                textView.setTextSize(15);
                                textView.setText(str);
                                linearLayout.addView(textView);
                            }
                        });
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                    try {
                        dout.close();
                        din.close();
                        socket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

        public void WriteToSocket(byte[] arr,int size) {
            try {
                dout.write(arr,0,size);
                dout.flush();
            }catch (IOException e) {
                e.printStackTrace();
                try {
                    dout.close();
                    din.close();
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

I make this class inside my activity class. I have another class inside my activity class for server, which extends thread and it works fine. Why does this client class crash and give me this error ?
this how I use it on my onCreate() function:

if (TYPE == 1) {
    serverMessages = new MyServerMessages(5002);
    Toast.makeText(this,"Room Started Wait clients To Join",Toast.LENGTH_LONG).show();
}
else {
    clientMessages = new MyClientMessages(deConvert(mycode)); // crash here
    Toast.makeText(this,"Connect To Room",Toast.LENGTH_LONG).show();
}

Upvotes: 0

Views: 145

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007349

why this client class crash and give me this error ?

Because you are creating a Socket and opening it in the constructor. Move that logic into run().

Upvotes: 2

Related Questions