【问题标题】:Java Android Send a data to server application never use a onPostExecute()Java Android 向服务器应用程序发送数据从不使用 onPostExecute()
【发布时间】:2016-11-21 09:32:46
【问题描述】:

我在向服务器发送数据时遇到问题,我使用套接字连接服务器。要将数据发送到服务器,我使用 AsyncTask。我有一个问题:

当应用程序发送数据时,我的应用程序看不到完成此操作,它从不使用onPostExecute()

这是我的代码:

public class MyClientTask extends AsyncTask<Void, Void, Void> {
        String dstAddress;
        int dstPort;
        String response = "";

        MyClientTask(String addr, int port) {
            dstAddress = addr;
            dstPort = port;
        }

        @Override
        protected Void doInBackground(Void... arg0) {
//            if (pingHost(1000)) {
            socket = null;
            try {
                socket = new Socket(dstAddress, dstPort);
                ByteArrayOutputStream byteArrayOutputStream =
                        new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

                byte[] theByteArray = message.getBytes();
                lengthMessage = (short) theByteArray.length;

                outputStream.writeByte((byte) 0xB);
                outputStream.writeByte((byte) 0xA);
                outputStream.writeByte((byte) 0xA);
                outputStream.writeByte((byte) 0xD);
                outputStream.writeShort(lengthMessage);
                outputStream.write(theByteArray);
                outputStream.writeShort(width);
                outputStream.writeShort(height);
                outputStream.writeInt(lengthbmp);
                outputStream.writeInt(lengthResizebmp);
                outputStream.writeShort(11);
                outputStream.write(imageInByte );
                outputStream.write(imageResizeInByte);
                outputStream.writeByte((byte) 0xB);
                outputStream.writeByte((byte) 0xE);
                outputStream.writeByte((byte) 0xE);
                outputStream.writeByte((byte) 0xF);

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                int bytesRead;
                InputStream inputStream = socket.getInputStream();

                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                    response += byteArrayOutputStream.toString("UTF-8");
                }

                outputStream.flush();
                outputStream.close();

            } catch (UnknownHostException e) {
                e.printStackTrace();
                isSuccsess = false;
                response = "UnknownHostException: " + e.toString();
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("la", "nie udało sie");
                isSuccsess = false;
                response = "IOException: " + e.toString();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (socket != null) {
                try {
                    socket.shutdownInput();
                    socket.shutdownOutput();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(isSuccsess){
                Toast.makeText(MainActivity.this, "Zdjęcie zostało wysłane !" , Toast.LENGTH_LONG).show();
                bm = null;
                clearEt();
                ivImage.setImageBitmap(bm);
            }
            else{
                Toast.makeText(MainActivity.this , "Nie udało się wysłać zdjęcia !" , Toast.LENGTH_LONG).show();
            }

            pbWheel.setVisibility(View.INVISIBLE);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pbWheel.setVisibility(View.VISIBLE);
        }
    }

【问题讨论】:

  • 请不要再次发布您的问题。更好地回答您第一篇帖子中的问题stackoverflow.com/questions/40679274/…
  • 你检查过控制台吗?我的意思是,有什么例外吗?
  • @mallaudin 也不例外

标签: java android sockets android-asynctask


【解决方案1】:

您可以使用AsyncTask&lt;Void,Void,Integer&gt;,并在doInBackground 中返回任何整数而不是null。

只需替换(在 doInBackground 中)

return null;

return 1;

并替换(在 AsyncTask 声明中)

public class MyClientTask extends AsyncTask<Void, Void, Void> {

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

【讨论】:

  • AsyncTask 第三个参数中除了 void 之外的任何类型都可以
  • 这不是真的。 AsyncTask&lt;Void,Void,Void&gt; 仍会调用 onPostExecute
  • 我假设使用 void 返回类型不会从 doInBackground 返回任何数据,因此不会调用 onPostExecute。我确信使用 Integer 是可行的。
  • 你猜错了。如果它没有返回任何数据,并不意味着我们不需要onPostExecute。最好不要发布假设。
  • 我已经从答案中删除了那部分
【解决方案2】:

请注意,getInputStream() 是一个阻塞调用。它将阻塞,直到它从服务器接收到数据。在您的情况下,您在调用 getInputStream() 之前将数据刷新到服务器,这将等待服务器的响应。现在你还没有向服务器发送任何数据(我猜你应该在你的情况下得到响应),套接字将阻塞等待输入的线程。

您应该在获取输入流并使线程休眠之前刷新数据。完成后还要关闭所有流,因为关闭流将关闭底层套接字。来自文档:

关闭返回的 OutputStream 将关闭关联的套接字。

关闭返回的 InputStream 将关闭关联的套接字。

outputStream.flush(); // flush it before sleep and don't close
// outputStream.close(); don't close it here
 try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                int bytesRead;
                InputStream inputStream = socket.getInputStream();

                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                    response += byteArrayOutputStream.toString("UTF-8");
                }
           

【讨论】:

    【解决方案3】:

    你会在这行之后执行吗?

    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        byteArrayOutputStream.write(buffer, 0, bytesRead);
                        response += byteArrayOutputStream.toString("UTF-8");
                    }
    

    在 AsyncTask 中使用 URLConnection 代替套接字。

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多