android - Is there any way to get upload progress correctly with HttpUrlConncetion -
android developers blog recommend use httpurlconnection
other apache's httpclient
(http://android-developers.blogspot.com/2011/09/androids-http-clients.html). take advice , problem in reporting file upload progress.
my code grab progress 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 = system.currenttimemillis(); if (now - lasttime >= mlistener.getprogressinterval()) { lasttime = now; if (!mlistener.onprogress(bytes, msize)) { break; } } } } out.flush(); } { closesilently(in); closesilently(out); }
this code excutes fast whatever file size, file still uploading server util response server. seems httpurlconnection
caches data in internal buffer when call out.write()
.
so, how can actual file upload progress? seems httpclient
can that, httpclient
not prefered...any idea?
i found explanation on developer document http://developer.android.com/reference/java/net/httpurlconnection.html
to upload data web server, configure connection output using setdooutput(true). best performance, should call either setfixedlengthstreamingmode(int) when body length known in advance, or setchunkedstreamingmode(int) when not. otherwise httpurlconnection forced buffer complete request body in memory before transmitted, wasting (and possibly exhausting) heap , increasing latency.
calling setfixedlengthstreamingmode()
first fix problem. mentioned this post, there bug in android makes httpurlconnection
caches content if setfixedlengthstreamingmode()
has been called, not fixed until post-froyo. use httpclient instead pre-gingerbread.
Comments
Post a Comment