java - Java异步任务/套接字连接问题

标签 java android sockets android-asynctask

我在Java中遇到异步任务问题:
我有一个移动应用程序,该应用程序将图像发送到我的服务器,然后从该服务器接收另一个图像,然后通过套接字/端口接收0到100之间的7个int值。所以我有一个AsnycTask,我在其中执行DoInBackground方法中的所有连接/发送/接收操作。图像工作正常,但是问题是int []:我有一个长度为7的probabilities [] int数组,在其中放置接收到的值(在DoInBackground中)。然后,在onPostExecute中,我调用需要这些值的我自己的函数。
现在的问题是:有时这些值在那里,有时Array仍然只有0(而且我可以肯定的是数据不仅是0)。
这是我的AsnyTask代码

    class ConnectTask extends AsyncTask<Void, Void, Void> {
                @Override
                protected Void doInBackground(Void... voids) {
                    try {
                        //Connect to my PC and send my image from my phone
                        Socket photoSocket = new Socket(IP_ADDRESS, PORT_NO);
                        DataOutputStream dos = new DataOutputStream(photoSocket.getOutputStream());
                        FileInputStream fis = new FileInputStream(pathname);
                        int size = fis.available();
                        byte[] data = new byte[size];
                        fis.read(data);
                        dos.write(data);
                        dos.flush();
                        fis.close();
                        dos.close();
                        photoSocket.close();
    
                        //now receive the other image from server
                        ServerSocket serverSocket = new ServerSocket(1235);
                        Socket incomingSocket = serverSocket.accept();
                        InputStream is = incomingSocket.getInputStream();
                        FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        byte[] aByte = new byte[40000];
                        int bytesRead;
    
                        while ((bytesRead = is.read(aByte)) != -1) {
                            bos.write(aByte, 0, bytesRead);
                        }
    
                        is.close();
                        fos.close();
                        bos.close();
                        serverSocket.close();
                        incomingSocket.close();
    
    
                        //now Receive Values (ints)
                        ServerSocket serverSocket2 = new ServerSocket(1236);
                        Socket incomingSocket2 = serverSocket2.accept();
                        InputStream inputstream = incomingSocket2.getInputStream();
                        byte[] data2 = new byte[7];
                        inputstream.read(data2);
                        //Fill array with received ints THIS IS SOMETIMES WORKING, SOMETIMES probabilities[] STAYS FILLED WITH 0s
                        for (int i = 0; i < data2.length; i++){
                            probabilities[i] = data2[i];
                        }
    
                        serverSocket2.close();
                        incomingSocket2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
    
                @Override
                protected void onPreExecute() {
                //irrelevant
                }
    
                @Override
                protected void onPostExecute(Void aVoid) {
                    File xaifile = new File(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
//...doing some UI stuff
//..................
//..................
                    //now call function where i replace picture. Since this is in the "postExecute" method, my probabilities[] should have been filled with data since its in the doInBackground method.
                    replacePicture(xaifile);
                }
            }
    
            ConnectTask connect =  new ConnectTask();
            Void[] param = null;
            //execute Async task
            connect.execute(param);

最佳答案

inputstream.read(data2)可能返回-1,这意味着不读取任何字节并且data2保持为零,因此如果读取返回-1,则进行处理并重试。

关于java - Java异步任务/套接字连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62898497/

相关文章:

java - 无法迭代并绑定(bind)具有相同端口和不同 IP 的套接字

c++ - 处理 Windows 套接字错误

java - 大海捞针阵列针

java - 将格式化字符串解析为数组

java - 从 xsd 文件导出类型列表

java - Android - 如何在 GridView 中选择一个项目?

android - 如何在一个 Activity 中运行两个 AsyncTasks?

java - 正则表达式匹配所有不以 X 开头或包含 Y 的内容

安卓图库和适配器

java - 谁能告诉我为什么在使用 SSL 时在 Java 中出现错误?