Android Java Socket。无法向服务器发送消息



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

<使用权限android:name="android.permission.INTERNET"/>

<使用权限android:name="android.permission.ACCESS_NETWORK_STATE";/>

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

我只需要在像这样的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

最新更新