javaandroidsocketsandroid-asynctask

Java Android Send a data to server application never use a onPostExecute()


I have a problem with send a data to server, to connect with server I used a socket. To send a data to server I use a AsyncTask. And I have a problem with :

when a application send a data , my app don't see a finish this action, it never use onPostExecute().

This is my code :

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

Solution

  • You can use AsyncTask<Void,Void,Integer>, and return any integer instead of a null in the doInBackground.

    Just replace (in the doInBackground)

    return null;
    

    with

    return 1;
    

    and replace (in the AsyncTask declaration)

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

    with

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