Android Developers Blog recommend to use HttpURLConnection
other than apache's HttpClient
(http://android-developers.blogspot.com/2011/09/androids-http-clients.html). I take the advice
and get problem in reporting file upload progress.
my code to grab progress is like this:
try {
out = conncetion.getOutputStream();
in = new BufferedInputStream(fin);
byte[] buffer = new byte[MAX_BUFFER_SIZE];
int r;
while ((r = in.read(buffer)) != -1) {
out.write(buffer, 0, r);
bytes += r;
if (null != mListener) {
long now = System.currentTimeMillis();
if (now - lastTime >= mListener.getProgressInterval()) {
lastTime = now;
if (!mListener.onProgress(bytes, mSize)) {
break;
}
}
}
}
out.flush();
} finally {
closeSilently(in);
closeSilently(out);
}
this code excutes very fast for whatever file size, but the file is actually still uploading to the server util i get response from the server. it seems that HttpURLConnection
caches all data in internal buffer when i call out.write()
.
So, how can i get the actual file upload progress? Seems like httpclient
can do that, but
httpclient
is not prefered...any idea?