0

Which is the best way to extract data from AsyncTask? Is there better way to manage data in background?

So far this is the function I'm using to make socket communication and change messages:

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

    MyClientTask(String endereco, int porta) {
        dstAddress = endereco;
        dstPort = porta;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            socket = new Socket(dstAddress, dstPort);
            socketStatus = true;

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int bytesRead;
            InputStream inputStream = socket.getInputStream();
            /*
             * notice:
             * inputStream.read() will block if no data return
             */
            response = "";
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
                response = byteArrayOutputStream.toString("UTF-8");
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "UnknownHostException: " + e.toString();
            socketStatus = false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "IOException: " + e.toString();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

As I can see it's fast to process, but how I can manage to extract "response" from this code using onPostExecute?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Possible duplicate of [How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) – OneCricketeer Nov 03 '16 at 19:12
  • Firstly, you need to actually return something from `doInBackground` so that the `onPostExecute` knows about it. Alternatively, there are likely libraries that make socket communication easier without AsyncTask – OneCricketeer Nov 03 '16 at 19:13
  • An [this answer](http://stackoverflow.com/a/38004234/2308683) describes that `AsyncTask` for a Socket operation at all, is probably not ideal. – OneCricketeer Nov 03 '16 at 19:16

1 Answers1

0

If I understand you correctly: There is somewhere a business class which wants to be informed about the received response.

So your async task has to notify this other class in some way, which could be done in the OnPostExecute method (unless the used AsyncTask offers itself such a mechanisme). If you have to implement it yourself: there are a lot of different possibilities, from simple Object wait/notify, over more sophisticated using a CompletableFuture, or just passing in (into your task class) an interface with a callback method.

Once your business class has been notified, it could e.g. access the response by a simple getter (which you still have to implement).

Heri
  • 4,368
  • 1
  • 31
  • 51