How to receive data on Android socket?
You can use the Socket class to receive data on Android. Here is a simple example code:
Socket socket = new Socket("服务器IP地址", 端口号);
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// 处理接收到的数据
}
socket.close();
It is important to note that this is just a simple example and may need appropriate modification and expansion based on specific requirements. Please make sure to close the socket connection at the right time. Additionally, this code is running on the main thread, so if you need to perform networking operations in the background, it is recommended to use asynchronous tasks or a thread pool to execute.