Reputation: 1107
Android: I am downloading a file from the internet using this method:
InputStream in = null;
try {
in = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = in.read(buffer)) != -1) {
try {
fout.write(buffer, 0, length);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
The problem is that when the internet is turned off, the read inoutstream stops reading on some device, but it continues reading on other devices! How to solve this problem? thanks in advance.
EDIT: I found the solution, I have to set a connection's timeout.
Upvotes: 1
Views: 287
Reputation: 15052
When you establish a connection, put that into a try-catch
to know if establishing a connection fails. Handle your problem in the catch
block. I don't exactly know how you are establishing the connection, so won't be able to suggest further.
Upvotes: 1