简体   繁体   English

使用单个 AsyncTask 向 Python 服务器发送数据,然后从 Python 服务器接收数据

[英]Using single AsyncTask to both send and then receive data to/from Python server

I want to send 40x40px blobs to my Python server, then process it there and send back a reply with id representing the image class (its an image classification task).我想将 40x40px 的 blob 发送到我的 Python 服务器,然后在那里处理它并发回一个带有代表图像类的 id 的回复(它是一个图像分类任务)。 I use AsyncTask and here comes a problem - the blob is sent to the server but then the part responsible for receiving the reply is not reached in my Android code.我使用 AsyncTask 并且出现了一个问题 - blob 被发送到服务器,但随后在我的 Android 代码中没有到达负责接收回复的部分。

I wonder whether it is correct to both send and then receive data in single AsyncTask.我想知道在单个 AsyncTask 中发送和接收数据是否正确。 I have read that tasks taking about <10 seconds are fine for this solution, so theoretically there should be no problem in my case.我已经读过大约 <10 秒的任务对于这个解决方案来说很好,所以理论上我的情况应该没有问题。

Here I enclose my code, for Client:在这里,我附上了我的代码,用于客户端:

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);
    }
}

And for python server:对于 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")

What is also important is that I call the AsyncTask.execute() in my onCameraFrame() method - once a while (not in every frame, only when my blob is sufficiently "stable", which happens rather rarely).同样重要的是,我在onCameraFrame()方法中调用AsyncTask.execute() - 一次(不是在每一帧中,只有当我的 blob 足够“稳定”时,这种情况很少发生)。

Apparently it got stuck on readUTF() part.显然它卡在了readUTF()部分。 Now it works:现在它的工作原理:

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

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

Now it returns 0 when I send "0" from the python server, so it works fine for me.现在当我从 python 服务器发送“0”时它返回 0,所以它对我来说很好用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java从Android向Web服务器发送和接收数据(POST和GET)的最佳方式? - Best way to send and receive data(POST and GET) from Android to web server using Java? 如何从客户端到服务器发送和接收多个数据 - How to send and receive multiple data from client to server Android 应用程序 - 从服务器发送和接收数据的问题 - Android app - problem with send and receive data from server Java 发送和接收来自另一台计算机中的 python 程序的数据 - Java send and receive data from python program in another computer TCP聊天程序如何从同一线程发送和接收数据? - How does TCP chat program both send and receive data from the same thread? 将数据从Android App发送到Google App Engine Python,Android App以及服务器端的Python代码都需要代码 - Send Data from Android App to Google App Engine Python, Need code for both Android App as well as the Python code on the server side 使用套接字发送和接收数据 - Using Sockets to send and receive data Java服务器处理数据-接收和发送数据 - Java server handling data - Receive and send data 如何使用AsyncTask(通过POST请求)将param值发送到Server并获取数据? - How to send param value to Server and get data using AsyncTask (by POST Request)? 如何从Android向PHP服务器localhost发送请求(字符串数据)并从服务器PHP接收响应结果 - How to send request(String data) from android to PHP server localhost and receive response result form server PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM