-1

Basically as stated above. I have 2 AsyncTasks, and Thread.sleep() crashes in one but works in the other when I add it.

here is the task that it works in

public class createConnection extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params){
        try{
            socket = new Socket(ip, port);
            PrintWriter out = new PrintWriter(socket.getOutputStream());
            InputStream is = socket.getInputStream();
        }
        catch (UnknownHostException e){
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

The task that makes it crash:

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

    @Override
    protected Void doInBackground(Void... params) {
        out.write(messageToSend);
        out.flush();
        msg.setText("message sent!");

        try{
            Thread.sleep(1000);

            msg.setText("before attempt:");
            byte[] buffer = new byte[4096];
            baos = new ByteArrayOutputStream(4096);

            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1){
                baos.write(buffer, 0, bytesRead);
            }

            String response = baos.toString("UTF-8");
            msg.setText(response);
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

My code consists of one button that creates a TCP connection with a Python server (on a Pi), and a second button which sends and receives text between the two. In the msg textbox, the app displays "message sent!" before it crashes, but does not reach "before attempt", which leads me to think that it is Thread.sleep() crashing the application. Does anyone have any idea why it's crashing or why it's working in one function but not the other?

KGhadiri
  • 49
  • 5
  • It's more likely that the `setText()` calls are crashing it. Don't try to manipulate `View`s directly from `doInBackground()`. In any case, you'll need to look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Apr 16 '18 at 00:52

1 Answers1

1

You can't call msg.setText(...) from inside doInBackground(). That will definitely cause an exception. You can use the progress mechanism of AsyncTask to post progress updates:

public class SR extends AsyncTask<Void,String,Void> { // <- NOTE TYPE CHANGE

    @Override
    protected Void doInBackground(Void... params) {
        out.write(messageToSend);
        out.flush();
        publishProgress("message sent!");

        try{
            Thread.sleep(1000);

            publishProgress("before attempt:");
            byte[] buffer = new byte[4096];
            baos = new ByteArrayOutputStream(4096);

            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1){
                baos.write(buffer, 0, bytesRead);
            }

            String response = baos.toString("UTF-8");
            publishProgress(response);
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void onProgressUpdate(String... progress) {
        msg.setText(progress[0]);
    }
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank you guys for helping me realize my mistake, I was able to get the code working now using onPostExecute – KGhadiri Apr 16 '18 at 02:51