Is there any way to get upload progress correctly with HttpUrlConncetion

守給你的承諾、 提交于 2019-12-21 03:48:31

问题


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?


回答1:


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.




回答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);


来源:https://stackoverflow.com/questions/18100096/is-there-any-way-to-get-upload-progress-correctly-with-httpurlconncetion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!