I'm trying to post a video file to our server and monitor it's progress. I followed the steps outlined in Can't grab progress on http POST file upload (Android) and that worked great, but with larger files my program will hang while writing to the output socket and subsequently cause my phone to lock the WiFi so I can't turn it on/off and then cause it to crash (after a short period).
So I attempted to write my own HTTPClient and it works, but also with intermittent success, still falling victim to the random crashes of the method outlined above. It seems this only occurs on files > 5MB, but I've had it die around 1.3MB and I've even had it successfully transfer a 13MB file. The fact that it's so random and sporadic is infuriating but I'm convinced there's some reason it's happening.
Here's my connection code:
socket.connect(new InetSocketAddress(host, port));
socket.setSendBufferSize(1024 * 65);
int bytesSent = 0;
PrintStream out = new PrintStream(socket.getOutputStream());
out.print(headersBuffer);
out.print(bodyBuffer);
bytesSent += headersBuffer.length() + headersBuffer.length();
byte[] bytes = new byte[1024 * 65];
int size;
while ((size = fileStream.read(bytes)) > 0) {
mListener.transferred(bytesSent);
Log.i(TAG, "bytes sent: " + bytesSent);
bytesSent += size;
out.write(bytes, 0, size); // Random freezes (/blocking?) on this line
out.flush();
}
Log.i(TAG, "Made it!");
out.print(lastBoundary);
out.flush();
I've used the debugger to see where it's getting to in the stack when the write just seems to block and it's the OSNetworkSystem.writeSocketImpl() function. That function just never returns...
So my next thought was - if the socket will just sit there, perhaps I can interrupt it and force it to close so at least the phone doesn't crash and the user can retry... I read up on force closing sockets in Android here (since it seems there are some problems): http://code.google.com/p/android/issues/detail?can=2&q=7933&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&id=7933
Basically what I did was create a Listener thread that looks at how many bytes have been transferred every 500ms and if there hasn't been a change, attempt to force close the socket by means of
socket.shutdownOutput();
socket.close();
However the socket returns that it is closed and everything proceeds to fail as outlined above.
Here's the general sequence of events in Logcat:
12-21 14:25:26.802 2234 2340 V UploadService: Bytes transferred: 5959800 of: 13191823
12-21 14:25:26.802 2234 2340 V UploadService: Bytes transferred: 5963896 of: 13191823
12-21 14:25:26.802 2234 2340 V UploadService: Bytes transferred: 5967992 of: 13191823
12-21 14:26:00.693 1262 1270 D WifiService: acquireWifiLockLocked: WifiLock{NetworkLocationProvider type=2 binder=android.os.Binder@45b48958}
12-21 14:26:11.083 1262 1289 D WifiHW : 'DRIVER LINKSPEED' command timed out.
12-21 14:26:21.130 1262 1500 D WifiHW : 'AP_SCAN 2' command timed out.
12-21 14:26:31.177 1262 1500 D WifiHW : 'SCAN' command timed out.
And after a few minutes the really bad stuff starts happening and the phone crashes!
Please help! Thank you.
EDIT: Works great over 3G - I'm going to try at home and see if its some sort of router issue. However - how can I catch this problem and prevent the phone from crashing?