[TIMOB-20483] Android HTTPClient onsendstream progress is wrong
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2016-10-20T22:56:26.000+0000 |
Affected Version/s | Release 5.2.0 |
Fix Version/s | Release 6.1.0 |
Components | Android |
Labels | android |
Reporter | Low Bird |
Assignee | Gary Mathews |
Created | 2016-02-24T12:40:23.000+0000 |
Updated | 2017-02-02T23:22:37.000+0000 |
Had the same issue when trying to upload a 3-5MB file with a self-build 5.4.0 git version. Progress was at 100% but the upload still took some time before onLoad was called. I thought it was my server who is not responding correctly. But it looks exactly like the issue you are describing.
Did some testing and found a solution: https://developer.android.com/reference/java/net/HttpURLConnection.html {noformat} 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. {noformat} So adding
client.setChunkedStreamingMode(0);
belowclient.setDoOutput(true);
and addingsetRequestHeader("Transfer-Encoding","chunked");
in TiHTTPClient.java fixed the problem. But I need to do some more testing before creating a PRPR: https://github.com/appcelerator/titanium_mobile/pull/8003 I've tried to use setFixedLengthStreamingMode(totalLength) but it seems that it is not the total length of the whole request. Some bytes where missing and it led to an error. So I stick to setChunkedStreamingMode() with a default value of 1024 (1kb) as mentinoned in different postings e.g. http://stackoverflow.com/a/26730636/5193915 Using the example code above worked fine with a 5MB file: progress and onload are in sync
[~michael] Thank you so much for the investigation and PR. I've yet to check setChunkedStreamingMode more but is there any downside to using that?
@msamah as far as I read it will disable the internal buffering (which is the problem in the current upload process) of the file in memory. And since Google recommends this as "best performance" in their docs it might be a good idea :) But I'm not 100% sure if there are side-effects with different servers. I've tested it with a apache/php host with a basic
move_uploaded_file()
function(y)
I can confirm that adding client.setChunkedStreamingMode(1024); fixes the issue. Thank you Michael G! However, I believe the root cause for all this is that the calculation of totalLength is wrong. Maybe it's worth to figure out why this is happening aswell...
Tested with :
With url to httpbin to make sure it works.
PR: https://github.com/appcelerator/titanium_mobile/pull/8003 has been reviewed and merged into master
Hi guys, is there any way for me to test it as ? Is it part of any continous build available yet? Thank you
sure: http://builds.appcelerator.com.s3.amazonaws.com/index.html#master
Hi Michael, thanks for link, I installed latest sdk v.6.0.0.v20160629231945 from Wed Jun 29, 2016 11:19:45 PM but I'm still facing error with ondatastream progress. I use Ti.Media.showCamera() to take a picture and in success event I use a JSONRPC module(https://github.com/wiistriker/Titanium-JSONRPC-Client, it uses basic xhr requests) to upload that picture to server. I create callback for onsendstream to log progress, but it logs 1 for many times(not usual float number progress). I updated node.js to v6.2.2, cleaned project and run fresh build. Can you please help me? Thank you
Verified the fix. The
onLoad()
callback gets called within a 1-2 seconds after download gets completed. Closing. Environment: Appc Studio : 4.7.0.201607250649 Ti SDK : 6.0.0.v20160817065403 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.6 Appc NPM : 4.2.7 Appc CLI : 6.0.0-24 Node: 4.4.4 Nexus 6 - Android 6.0.1Had to reopen and undo this due to this fix causing TIMOB-23852. This needs to be revisited.
Hi guys, any news on this? Thanks
[~max87] Sorry. This had to be reopened to be revisited. It's not lined up for any sprint yet.
master: https://github.com/appcelerator/titanium_mobile/pull/8522
[~gmathews], I am seeing some difference between the time the progress bar fills up & I see the success. For a 5MB file the difference is 14 sec. For a 20 MB file the difference is 50 sec :
I don't think this is acceptable. Appc Studio : 4.8.1.201611291132 SDK Version : 6.1.0.v20161202104721 Mac OS Version : 10.12 Xcode Version : Xcode 8.1 Build version 8B62 Appc CLI AND Appc NPM : {"NPM":"4.2.9-1","CLI":"6.1.0-302"} Ti CLI : 5.0.11 Alloy : 1.9.4 Node : v4.6.0 Device: running 7.1Pixel
Tested with this environment: NPM Version: 2.15.9 Node Version: 4.5.0 Mac OS: 10.12.1 Appc CLI: 6.1.0 Appc CLI NPM: 4.2.8 Titanium SDK version: 6.1.0.v20170202120400 Appcelerator Studio, build: 4.8.1.201612050850 Android Device: 6.0.1 I tested this with SDK 6.0.0.GA, and found that a 16 Mb file took about 15 seconds to upload, while the progress bar hit "1" at about 8 seconds. With SDK 6.1.0, the upload was approximately 12 seconds, while the progress bar hit "1" at 10 seconds. The variability in upload time is likely due to variability due to the Wi-Fi connection. I tested this several times, and I consistently found that upload completion was with 2-3 seconds after the progress bar hit "1".