Java客户端无法接收Python服务器发送的位置列表数据求助
位置提醒APP:Java客户端无法接收Python服务器返回的位置列表数据
我正在开发一款基于位置提醒的待办APP,核心功能是用户靠近指定位置时推送对应任务。主界面用ListView展示用户添加的所有位置,这些位置以JSON格式存储在数据库中。
用户登录流程正常:数据发往Python服务器,校验账号密码后返回消息,随后跳转至主界面。但在主界面onCreate方法中获取当前登录用户的位置列表时,Java客户端无法接收到服务器发送的数据。
相关代码
SocketTask类(服务器收发数据)
package com.example.yearprojectfinal; import android.os.AsyncTask; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; public class SocketTask extends AsyncTask<String, Void, String> { private final static String IP_ADDRESS = "192.168.1.218"; private final static int PORT = 50001; private Socket socket; private String sendingStr = ""; private String receivingStr = ""; public SocketTask(String str1) { this.sendingStr = str1; } @Override protected String doInBackground(String... arrStrings) { try { this.socket = new Socket(IP_ADDRESS, PORT); send(this.sendingStr); receive(); this.socket.close(); } catch (Exception e) { Log.e("Exception", e.toString()); } return this.receivingStr; } private void send(String sendingStr) { try { OutputStream outputStream = this.socket.getOutputStream(); byte[] bytes = sendingStr.getBytes(StandardCharsets.UTF_8); outputStream.write(bytes); outputStream.flush(); System.out.println("sent"); } catch (Exception e) { Log.e("Exception", e.toString()); } } private void receive() { try { System.out.println("trying to receive"); InputStream inputStream = socket.getInputStream(); System.out.println(inputStream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int c; while ((c = inputStream.read()) != -1) { baos.write((char) c); } System.out.println("hello"); byte[] bytes = baos.toByteArray(); receivingStr = new String(bytes, StandardCharsets.UTF_8); inputStream.close(); Log.d("str received", receivingStr); } catch (Exception e) { Log.e("Exception", e.toString()); } } }
该类负责与服务器收发数据,除获取位置列表场景外,其他功能均正常。
调用SocketTask的工具函数
private String sendAddLocationDataToServer(String strToSend) { try { SocketTask send1 = new SocketTask(strToSend); String strReceived = send1.execute().get(); return strReceived; } catch (ExecutionException | InterruptedException e) { Log.e("Exception", e.toString()); } return "something went wrong with execution"; }
获取用户位置列表的调用代码
public ArrayList<Location> getLocationListForUser() { String dataStrToSend = "requesting location list," + connectedUsername; // 发送长度头部 String resultStr = sendAddLocationDataToServer( String.format("%05d", activityName.length()) + activityName + String.format("%05d", dataStrToSend.length()) + dataStrToSend); Gson gson = new Gson(); Type listType = new TypeToken<ArrayList<Location>>(){}.getType(); ArrayList<Location> locationsListByUsername = gson.fromJson(resultStr, listType); return locationsListByUsername; }
排查情况
已确认Python服务器运行正常且数据已发送,但Java客户端在SocketTask的receive方法中执行以下代码时出现异常:
int c; while ((c = inputStream.read()) != -1) { baos.write((char) c); }
日志仅输出:
2022-12-21 15:55:43.640 14239-14242/com.example.yearprojectfinal I/earprojectfina: Thread[3,tid=14242,WaitingInMainSignalCatcherLoop,Thread*=0x7a3c35e33a60,peer=0x13200238,"Signal Catcher"]: reacting to signal 3 2022-12-21 15:55:43.640 14239-14242/com.example.yearprojectfinal I/earprojectfina: 2022-12-21 15:55:43.696 14239-14242/com.example.yearprojectfinal I/earprojectfina: Wrote stack traces to tombstoned
需求
求解决思路或排查方案。
内容的提问来源于stack exchange,提问作者nortain32
相关产品推荐
相关产品推荐

