Is there any way to get upload progress correctly with HttpUrlConncetion

后端 未结 2 726
滥情空心
滥情空心 2021-02-14 05:52

Android Developers Blog recommend to use HttpURLConnection other than apache\'s HttpClient (http://android-developers.blogspot.com/2011/09/androids-htt

2条回答
  •  清酒与你
    2021-02-14 06:24

    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.

提交回复
热议问题