I am desperatly trying to find a soulution for WiFi/GSM connection switch while uploading files via HttpUrlConnection. I have seen people struggling with similar problems but still couldn't find a working solution. Here is the code:
try {
/* Opening file stream and so... */
URL url = new URL(url_to_send);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// connection.setConnectTimeout(1000); // doesn't have any effect
// connection.setReadTimeout(1000);
connection.setChunkedStreamingMode(maxBufferSize);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data; charset=utf-8; boundary=" + BOUNDARY);
bytesAvailable = file_input.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = file_input.read(buffer, 0, bufferSize);
already_send += bytesRead;
while (bytesRead > 0) {
/* Updating some progressbar values */
/* HERE IS THE LINE WHERE THREAD HANGS */
output_byte.write(buffer, 0, bufferSize);
bytesAvailable = file_input.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = file_input.read(buffer, 0, bufferSize);
already_send += bytesRead;
}
output_byte.writeBytes(endBoundary);
output_byte.flush();
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
} catch (FileNotFoundException e) {
Log.e(TAG, "Uploaded file not found");
e.printStackTrace();
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed URL");
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "Uploading problem: " + e.toString());
e.printStackTrace();
} finally {
/* closing connection & streams */
}
The problem is that whenever i switch from WiFi to GSM or from GSM to WiFi during the uploading loop the Thread freezes on the line marked in the code: output_byte.write(buffer, 0, bufferSize);
I have tried using connection.disconnect() which should throw some kind of a IOException (it does in normal situations but not in this particular case)
I have tried using OutputStream.close() called from second thread but it also hangs the second (main UI) thread making me unable to even close the app.
I have been searching for something like connection.isAvailable() which I could call before the ouptut_byte.write but I couldn't find anything useful in HttpUrlConnection class.
I have tried monitoring the WiFi state but the uploading thread seems to be faster than wifi state change and it blocks before I am able to provide it the current wifi state change. (also tried the ConnectivityManager - no difference)
Finally I have tried to kill the thread by adding volatile flag to my Thread variable and using Thread.interrupt() - no effect. Thread is still alive and it's state is "RUNNING".
Have you got any ideas how Can I solve the problem? I would be really glad if I could catch any type of exception in this thread after is blocks.