java Android从python服务器中的socket接收消息
在使用此代码发送消息之后,我尝试从python服务器获取消息
我在Android上编写了代码,因此每次单击按钮时,该代码都会被激活并从新客户端连接,Python会将消息发送回所有登录过的参与者,并发送到新客户端
如何从服务器接收消息
我的代码:
class Send extends AsyncTask<String, Void, Void> {
public Socket socket; // Create socket
public PrintWriter printWriter; // Create print writer
protected Void doInBackground(String... strings) {
String command = strings[0]; // Set the command
try {
socket = new Socket("10.0.0.2", 13131); // Set socket connection
printWriter = new PrintWriter(socket.getOutputStream()); // Set the print writer with socket properties
printWriter.write(command); // Send the command to server
printWriter.flush(); // Clear the line
socket.close();
}
catch (UnknownHostException e) {e.printStackTrace();} // Error exception
catch (IOException e) {e.printStackTrace();} // Error exception
return null;
}
}
我试过了
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
System.out.println(line);
我得到一个错误异常:
I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils
我如何接收这些信息强>
我的python服务器:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set the socket
server.bind((socket.gethostname(), 13131)) # Set socket properties
server.listen()
(client, (ipNum, portNum)) = server.accept() # Accept to new client
print("Phone connected")
while True:
server.listen()
(client, (ipNum, portNum)) = server.accept() # Accept to new clients (Accept to new command from the phone)
Clients.append(client)
message = str(client.recv(32).decode()) # Set the command as string
if(message != ""): # Checks if a command has been sent
print("Client: " + message) # Print the command
Command(message.lower()) # Process the command
for Client in Clients:
Client.send(str(BackMessage).encode())
Clients.remove(Client)
print("Server: " + BackMessage) # Print the BackMessage
else:
for Client in Clients:
Client.send(str(BackMessage).encode())
Clients.remove(Client)
time.sleep(0.05) # Sleep for 0.05 seconds
# 1 楼答案
CTA=China Type Approval,这是联发科为测试目的在Android中添加的东西
您的错误发生在DriverManager中。getConnection()可能使用Android的libcore中的okhttp、apache http或Socket类来执行请求
联发科修补了这些库,以添加对HTTP请求的控制。它尝试动态加载/system/framework/mediatek-cta中定义的一些方法。jar但它可能不存在,或者在android设备的文件系统上无法访问
我看到了5种解决方案:
如果它不能修复1,那么第二个解决方案似乎很简单。因为它的操作系统和硬件相关的问题
参考:https://stackoverflow.com/a/54985015/9416473