安卓 Java 套接字。无法向服务器发送消息

时间:2021-06-22 12:03:15

标签: java android

好吧,首先,我对 java 和 android 编码还很陌生。我正在使用套接字。只是想知道我的客户端没有将消息发送到服务器。我已经设置了像这样显示的权限。

这是 logcat 错误。

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.

这是我的服务器代码!

            @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;
    }

这是我的客户端代码

 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);
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我只需要像这样将新线程放入 Onclick 函数中

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();

            }
        });
    }

感谢https://android-developers.googleblog.com/2009/05/painless-threading.html

相关问题