Project: titanium
Issue TIMOB-18870: Android: HTTPClient Onload Callback handles GET request incorrectly
h6.Issue Description When an user submits, in the 'load' event of the webview, document download functionality is initiated an completed. Contents are written to a file and fetched back and displayed in the webview. We are facing the below issues in android (works fine in iOS): 1. When calling HTTPClient document is not properly downloaded. The actual size of the document is around 10MB while the downloaded size is around 6KB 2. If the document download is initiated once, the login screen doesn’t render on the mobile app until we re-install the app. We have also tried to disable cache. Also a forbidden error occurs only when trying from within the app. If you hit the same url from Chrome browser, it works with the credentials used. h6.Use Case To download a remote document. The remote url will first render a Login screen; after successful login, the same url will render the actual document. h6.Android 4.0.4 device output: {code} 05-04 15:27:03.398: I/TiAPI(9530): 0.35535908174344655 05-04 15:27:03.398: I/TiAPI(9530): 0.8045602605863192 05-04 15:27:03.437: I/TiAPI(9530): 1 05-04 15:27:03.447: I/TiAPI(9530): onload called, readyState = 4 05-04 15:27:03.447: I/TiAPI(9530): download file onload {code} h6.Android 4.4.4 device output: {code} 05-04 15:48:41.077: I/TiAPI(7104): 1 05-04 15:48:41.079: I/TiAPI(7104): onload called, readyState = 4 05-04 15:48:41.079: I/TiAPI(7104): download file onload {code} h6.iOS 8.2 device output: {code} [DEBUG] New scheme: { URL: https://keysight-preprod.assetserv.com/das/UI/Login } [DEBUG] New scheme: { URL: https://keysight-preprod.assetserv.com/r2/servlet/dload/Leveraging-Big-Data.pdf?force_dload=true&confirm_license=true&id=4977 } [INFO] webViewonload - else [INFO] download file {code} h6.Test Case When we open the link it will ask for login, the login username and password it's been set automatically. Just hit the login button. Then the content will be downloaded fully and open. {code} var win = Ti.UI.createWindow(); win.addEventListener('open', function() { downloadKF(); }); var testWebView = Ti.UI.createWebView({ height : Ti.UI.FILL, width : Ti.UI.FILL }); testWebView.addEventListener('load', webViewonload); testWebView.addEventListener('error', webViewOnError); win.add(testWebView); win.open(); var loggedin = false; function downloadKF(e) { testWebView.url = url; } function autoLoginUser() { if (_emailID && _password) { testWebView.evalJS("document.getElementById('IDToken1').value = '" + _emailID + "'"); testWebView.evalJS("document.getElementById('IDToken2').value = '" + _password + "'"); testWebView.evalJS("LoginSubmit('Log In')"); loggedin = true; } } function webViewonload(e) { if (loggedin == false) { Ti.API.info('onload ' + e.source.html); setTimeout(function(e) { Ti.API.info('called autologin'); autoLoginUser(); }, 5); } else { Ti.API.info('webViewonload - else'); setTimeout(function(e) { downloadFile(); }, 1000); } } function webViewOnError(e) { Ti.API.info(' e.error code ' + e.errorCode); if (e.errorCode == -1015) { var _errorText = 'Incomplete download. \n(Error details: ' + e.message + ', Error code: ' + e.errorCode + '). \nPage will refresh.'; reloadInASecondWithMessage(_errorText); } } function reloadInASecondWithMessage(errorText) { testWebView.html = errorText; setTimeout(function incompleteMessageHandler() { testWebView.url = url; }, 1000); } function downloadFile() { Ti.API.info('download file'); var xhr = Titanium.Network.createHTTPClient({ onload : function(e) { Ti.API.info('onload called, readyState = ' + this.readyState); Ti.API.info('download file onload'); if (Ti.Filesystem.isExternalStoragePresent()) { sdPath = 'file:///sdcard/'; var folder = Ti.Filesystem.getFile(sdPath, 'Testing'); if (!folder.exists()) { folder.createDirectory(); } newPath = 'file:///sdcard/Testing/' + fileName; tmpFile = Ti.Filesystem.getFile(newPath); tmpFile.write(this.responseData); openFile(); } else { alert('No external storage present'); } }, timeout : 50000, onerror : function(e) { Ti.API.info('Error: ' + JSON.stringify(e)); }, ondatastream : function(e) { Ti.API.info(e.progress); } }); xhr.open('GET', url); xhr.send(); } function openFile() { var tmpFile = Ti.Filesystem.getFile('file:///sdcard/Testing/' + fileName); if (tmpFile.exists()) { Ti.API.info('tempfile: ' + tmpFile + ' ___ ' + JSON.stringify(tmpFile)); testWebView.backgroundColor = 'red'; testWebView.setData(tmpFile.nativePath); } else { Ti.API.info('File not found'); } } {code}
Story points: 8