【问题标题】:Using single AsyncTask to both send and then receive data to/from Python server使用单个 AsyncTask 向/从 Python 服务器发送和接收数据
【发布时间】:2020-02-29 15:49:09
【问题描述】:

我想将 40x40px 的 blob 发送到我的 Python 服务器,然后在那里对其进行处理并返回一个回复,其中 id 代表图像类(它是一个图像分类任务)。我使用 AsyncTask 并且出现了一个问题 - blob 被发送到服务器,但是在我的 Android 代码中没有到达负责接收回复的部分。

我想知道在单个 AsyncTask 中发送和接收数据是否正确。我已经读过这个解决方案大约需要不到 10 秒的任务,所以理论上我的情况应该没有问题。

在这里我附上我的代码,供客户使用:

public class ServerConnectAsyncTask extends AsyncTask<Void, Void, Integer> {

    private AsyncTaskResultListener asyncTaskResultListener;
    private Socket socket;
    private Mat img;

    ServerConnectAsyncTask(Mat blob, Context c) throws IOException {
        img = blob;
        asyncTaskResultListener = (AsyncTaskResultListener) c;
    }

    @Override
    protected Integer doInBackground(Void... voids) {
        MatOfByte buf = new MatOfByte();
        Imgcodecs.imencode(".jpg", img, buf);
        byte[] imgBytes = buf.toArray();

        try {
            socket = new Socket("192.168.0.109",8888);
            DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
            DataInputStream din = new DataInputStream(socket.getInputStream());

            dout.write(imgBytes);
            dout.flush();

            String str = din.readUTF();     // it seems that it doesn't reach this line

            dout.close();
            din.close();
            socket.close();

            return Integer.valueOf(str);
        } catch (IOException e) {
            e.printStackTrace();
            return 99;
        }
    }

    @Override
    protected void onPostExecute(Integer imgClass) {
        asyncTaskResultListener.giveImgClass(imgClass);
    }
}

对于python服务器:

HOST = "192.168.0.109"
PORT = 8888

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

s.listen(10)

while True:
    conn, addr = s.accept()
    print("Got connection from", addr)

    msg = conn.recv(4096)

    buf = np.frombuffer(msg, dtype=np.uint8).reshape(-1, 1)
    img = cv2.imdecode(buf, 0)
    cv2.imwrite("output.jpg", img)            # here I save my blob correctly

    if msg:
        message_to_send = "0".encode("UTF-8")     # then I send my "predicted" image class
        conn.send(message_to_send)
    else:
        print("no message")

同样重要的是,我在 onCameraFrame() 方法中调用 AsyncTask.execute() - 一次(不是在每一帧中,仅当我的 blob 足够“稳定”时,这种情况很少发生)。

【问题讨论】:

  • wonder whether it is correct to both send and then receive data in single AsyncTask. 是正确的。而且……如果你不这样做会很奇怪。
  • the part responsible for receiving the reply is not reached in my Android code.。请告诉最后执行的是哪条语句。
  • 或者android在尝试读取utf字符串时挂起?你舒尔 python 发送一个 utf 字符串吗?不是“正常”字符串?
  • msg = conn.recv(4096) Android 是否准确发送了 4096 字节?
  • Python 将字符串编码为 utf-8,android 应该尝试将其读取为普通字符串。您不能为此使用 .readUTF()。

标签: java android android-asynctask


【解决方案1】:

显然它卡在了readUTF() 部分。现在可以了:

DataInputStream din = new DataInputStream(socket.getInputStream());
int str = din.read();
char sign = (char) str;

dout.close();
din.close();
socket.close();
return Character.getNumericValue(sign);

现在当我从 python 服务器发送“0”时它返回 0,所以它对我来说很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多