JOCKEY
JOCKEY

Reputation: 53

Android Java Socket. Can't send message to server

Alright first off all, I'm fairly new to java and android coding. I'm working with socket. Just want to know my client didn't send the message to server.I've already set permission to manifest like this.

<uses-permission android:name="android.permission.INTERNET" / >

<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" / >

This is the logcat error.

2021-06-22 21:36:23.635 3273-3273/com.example.charactercounter I/System.out: [socket]:check permission begin!

2021-06-22 21:36:23.635 3273-3273/com.example.charactercounter W/System: ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
2021-06-22 21:36:23.637 3273-3273/com.example.charactercounter I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils

2021-06-22 21:36:23.638 3273-3273/com.example.charactercounter I/System.out: data error here

2021-06-22 21:36:23.638 3273-3273/com.example.charactercounter I/System.out: android.os.NetworkOnMainThreadException

2021-06-22 21:36:39.102 3273-3273/com.example.charactercounter I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.

2021-06-22 21:36:39.103 3273-3414/com.example.charactercounter I/GED: ged_boost_gpu_freq, level 100, eOrigin 2, final_idx 29, oppidx_max 29, oppidx_min 0

2021-06-22 21:36:43.643 3273-3414/com.example.charactercounter D/Surface: Surface::disconnect(this=0x7a8c40c000,api=1)


2021-06-22 21:36:43.663 3273-3273/com.example.charactercounter V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@28a3868, this = DecorView@5597d05[MainActivity]

2021-06-22 21:36:43.713 3273-3273/com.example.charactercounter I/Choreographer: Skipped 2 frames!  The application may be doing too much work on its main thread.

This is my server code !

            @Override
    protected Void doInBackground(Void... params) {
        Log.d("STATE", "onClick: This been clicked");
        TextView connection = findViewById(R.id.connection);
        TextView connection1 = findViewById(R.id.out_put_data);
        try {
            ServerSocket server_socket=new ServerSocket(7777);
            connection.setText("Server running on port:"+ server_socket.getLocalPort());

            Socket socket_name=server_socket.accept();//establishes connection
            DataInputStream dis=new DataInputStream(socket_name.getInputStream());
            String str= dis.readUTF();
            count_OccurrencesStr(str);
            server_socket.close();
        } catch (Exception e) {
            Log.d("Error", "onClick: Error on connecting server");
            System.out.println(e);
        }
        return null;
    }

And this is my client code

 public void submit(){
    TalkToServer tserver = new TalkToServer();
    tserver.execute();
    Button submit = findViewById(R.id.submit_btn);
    TextView text_output = findViewById(R.id.out_put_data);
    EditText editText = findViewById(R.id.data_field);

    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                System.out.println("data error here");
                Socket s = new Socket("127.0.0.1",7777);
                System.out.println("data error here1");
                DataOutputStream dout=new DataOutputStream(s.getOutputStream());
                String msg = "abbccc";
                dout.writeUTF(msg);
                dout.flush();
                dout.close();
                s.close();
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("data error here");
                System.out.println(e);
            }
        }
    });
}

Upvotes: 0

Views: 961

Answers (1)

JOCKEY
JOCKEY

Reputation: 53

All I just need is to put new Thread inside Onclick function like this

public void submit(){
        Button submit = findViewById(R.id.submit_btn);
        TextView text_output = findViewById(R.id.out_put_data);
        EditText editText = findViewById(R.id.data_field);

        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("data error here");
                            Socket s = new Socket("127.0.0.1",7777);
                            System.out.println("data error here1");
                            DataOutputStream dout=new DataOutputStream(s.getOutputStream());
                            String msg = "abbccc";
                            dout.writeUTF(msg);
                            dout.flush();
                            dout.close();
                            s.close();
                        } catch (Exception e) {
                            // TODO: handle exception
                            System.out.println("data error here");
                            System.out.println(e);
                        }
                    }
                }).start();

            }
        });
    }

Thanks to https://android-developers.googleblog.com/2009/05/painless-threading.html

Upvotes: 0

Related Questions