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?