Java 无法使用套接字从Android应用程序向Python服务器发送字节数据
我已经尽了一切努力来让它工作。基本上,我有一个Android应用程序,它通过本地网络连接从基于Python的服务器接收数据。我可以接收数据,没问题。但是,当我尝试发回数据时,应用程序崩溃,Python服务器接收到空白数据。我尝试过几种不同的方法,但没有成功。下面是我为接收消息而编写的Python方法:Java 无法使用套接字从Android应用程序向Python服务器发送字节数据,java,android,python,sockets,tcp,Java,Android,Python,Sockets,Tcp,我已经尽了一切努力来让它工作。基本上,我有一个Android应用程序,它通过本地网络连接从基于Python的服务器接收数据。我可以接收数据,没问题。但是,当我尝试发回数据时,应用程序崩溃,Python服务器接收到空白数据。我尝试过几种不同的方法,但没有成功。下面是我为接收消息而编写的Python方法: def checkReply(self): reply = "no reply yet" self.conn.settimeout(1) try: t
def checkReply(self):
reply = "no reply yet"
self.conn.settimeout(1)
try:
test = self.conn.recv(1024)
except:
self.conn.timeout;
print("I failed to hear this") #Debug to help see if I have heard an incomming message
try:
data = test.decode()
reply = data
except:
print("I failed to decode this") #Debug to help see if I could not decode an incomming message
print(reply)
self.conn.settimeout(0)
我的Android应用程序上的客户端如下所示:
public class Client extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "No data has been sent yet";
TextView textResponse;
Socket socket = null;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice: inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response = byteArrayOutputStream.toString("Ascii");
}
byteArrayOutputStream.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
protected String getSite(){
return response;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void returnMsg(){
try (DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream())) {
byte[] buf = "hello".getBytes("UTF-8");
outToClient.writeBytes("Test");
outToClient.flush();
} catch (IOException e) {}
}
公共类客户端扩展异步任务{
字符串地址;
国际数据传输端口;
String response=“尚未发送任何数据”;
文本视图文本响应;
套接字=空;
客户端(字符串地址、int端口、TextView textResponse){
dstAddress=addr;
dstPort=端口;
this.textResponse=textResponse;
}
@凌驾
受保护的Void doInBackground(Void…arg0){
套接字=空;
试一试{
套接字=新的套接字(dstAddress,dstPort);
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream(
1024);
字节[]缓冲区=新字节[1024];
int字节读取;
InputStream InputStream=socket.getInputStream();
/*
*注意:如果没有数据返回,inputStream.read()将被阻止
*/
而((bytesRead=inputStream.read(缓冲区))!=-1){
写入(缓冲区,0,字节读取);
response=byteArrayOutputStream.toString(“Ascii”);
}
byteArrayOutputStream.flush();
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
response=“UnknownHostException:”+e.toString();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
response=“IOException:+e.toString();
}最后{
if(套接字!=null){
试一试{
socket.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
setText(response);
super.onPostExecute(结果);
}
受保护的字符串getSite(){
返回响应;
}
@RequiresApi(api=Build.VERSION\u CODES.KITKAT)
public void returnMsg(){
try(DataOutputStream outToClient=newdataoutputstream(socket.getOutputStream())){
byte[]buf=“hello”.getBytes(“UTF-8”);
outToClient.writeBytes(“测试”);
outToClient.flush();
}捕获(IOE){}
}
}
我必须在物理设备上进行测试,因此我没有日志来跟踪错误消息。任何帮助都将不胜感激。谢谢,我设法解决了这个问题。在尝试通过套接字发回任何数据之前,我需要刷新缓冲区堆栈跟踪/错误消息?抱歉,刚刚编辑了这里的帖子。我正在物理设备上测试,因此我没有要跟踪的日志。EADB将为您获取日志跟踪。我用来测试的设备无法连接到Android Studio,我正在开发的PC无法运行虚拟机,因此我被卡住了:/n您能在设备上启用USB调试吗?如果是这样,请执行此操作,然后运行
adb logcat
。