13

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?

toki
  • 411
  • 4
  • 14
  • 1
    you get a massive amazing +1 for being one of the sensible developers not using apache's client. Some of the worst networking decisions in Android have been seen on this site in the name of the apache client. Secondly, typically progress is relevant uploading a large file. How big is your file? If so, is chunked transfer encoding appropriate for your file? – Tom Aug 07 '13 at 10:36
  • @Tom My app needs to support uploading file not larger than 30m, and the server side doesn't support chunked transfer encoding now... – toki Aug 07 '13 at 17:48
  • @Toki this is old but in case you were wondering you misspelled connection in line 2. – charliebeckwith Mar 18 '15 at 15:33

2 Answers2

15

I found the explanation on developer document http://developer.android.com/reference/java/net/HttpURLConnection.html

To upload data to a web server, configure the connection for output using setDoOutput(true).
For best performance, you should call either setFixedLengthStreamingMode(int) when the body length is known in advance, or setChunkedStreamingMode(int) when it is not. Otherwise HttpURLConnection will be forced to buffer the complete request body in memory before it is transmitted, wasting (and possibly exhausting) heap and increasing latency.

Calling setFixedLengthStreamingMode() first fix my problem. But as mentioned by this post, there is a bug in android that makes HttpURLConnection caches all content even if setFixedLengthStreamingMode() has been called, which is not fixed until post-froyo. So i use HttpClient instead for pre-gingerbread.

Community
  • 1
  • 1
toki
  • 411
  • 4
  • 14
  • This is the most helpful thing I've read on StackOverflow concerning this problem. So many people just tracked the write to the buffer and thought they solved the problem. – Brian Nov 15 '13 at 22:02
  • @toki, what's the status on this bug now? – avismara Aug 22 '16 at 11:56
-2

use Asynctask to upload file to upload your file to server and create a Progressdialog

1) run your code in

 doinbackground(){
    your code here..
}

2) update the progress in

publishProgress("" + (int) ((total * 100) / lenghtOfFile));
    //type this in the while loop before write..

3) and On updating the progress

protected void onProgressUpdate(String... progress) {
            Progress.setProgress(Integer.parseInt(progress[0]));
        }

4) dismiss the progress in

protected void onPostExecute(String file_url) {
            dismissDialog(progress);
Prakhar
  • 2,270
  • 19
  • 26
  • I don't think the OP is having problems making a UI for this. The OP wants to get a reliable interpretation of how much of a stream has been read off onto the socket. Where does "total" come from? – Tom Aug 07 '13 at 10:34
  • Tom is right, I want a way to get the network transmission progress when uploading my file, which is exactly how much of bytes have been sent to the server on socket layer. – toki Aug 07 '13 at 18:03