Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17625] iOS: HttpClient with async as false not responding

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionInvalid
Resolution Date2014-09-05T21:27:00.000+0000
Affected Version/sRelease 3.3.0, Release 3.4.0
Fix Version/sn/a
ComponentsiOS
LabelssupportTeam
ReporterMarco Cota
AssigneeIngo Muschenetz
Created2014-09-05T00:07:38.000+0000
Updated2017-03-21T20:59:04.000+0000

Description

Issue

When setting the async parameter as false in the HttpClient the callbacks unload and on error are not being called in Ti SDK3.3.0 and 3.4.0, the same code in SDK 3.2.3.GA the callback are correctly executed.

Test code

Test case attached (network.js)

app.js

var win = Ti.UI.createWindow({
	backgroundColor:'white'
});

var xhr = require('commonjs/xhr');
var url = "http://roi-stg.mcd.com/mobile/localization/au/v3.1/iPhone/image_resources.zip";
var sync = true;

function doCall(_url,_method,_sync){
		xhr.doCALL(_method,_url,{},callback,_sync);
}

function callback(_args) {
	alert("HTTPClient " + _args);
    Ti.API.info("HTTPClient " + _args);
}

var view = Ti.UI.createView({
	top:75,
	layout:'vertical'
});

var bt_HEAD = Ti.UI.createButton({
	title: "XHR HEAD"
});
var view2 = Ti.UI.createView({
	layout:'horizontal'
});
var label = Ti.UI.createLabel({
	text:'SYNC',
	width:Ti.UI.SIZE
});
var basicSwitch = Ti.UI.createSwitch({
  value:true // mandatory property for iOS 
});
view2.add(label);
view2.add(basicSwitch);

basicSwitch.addEventListener('change',function(e){
  Ti.API.info('Switch value: ' + basicSwitch.value);
  sync = basicSwitch.value;
});

bt_HEAD.addEventListener('click',function(e){
	doCall(url,'HEAD',sync);
});

view.add(bt_HEAD);
view.add(view2);
win.add(view);
win.open();

xhr.js

exports.doCALL = function (_method,_url, _params, _callback,_sync){
	// XHR POST
	var xhr = Titanium.Network.createHTTPClient();
	xhr.onload = function(e)
	{
	  _callback(this.responseText);
	};
	xhr.onerror = function(e)
	{
	  _callback('XHR >>> '+JSON.stringify(e));
	};
	xhr.open(_method, _url, _sync);
	xhr.send();
};

Steps to repro

1. Run test case 2. Use the Sync switch and set to false 3. Press XHR HEAD Expected Result: An alert showing the result of the httpClient call. Actual Result: No alert is shown as the callbacks are not called.

Attachments

FileDateSize
network.zip2014-09-05T00:07:38.000+00002455721

Comments

  1. Ingo Muschenetz 2014-09-05

    Is this not a duplicate of TIMOB-17432?
  2. Pedro Enrique 2014-09-05

    If you're making a SYNC call, there are no callbacks, the code will wait for the "send" call to be done. Try this:
       exports.doCALL = function (_method,_url, _params, _callback,_sync){
       	// XHR POST
       	var xhr = Titanium.Network.createHTTPClient();
       	xhr.onload = function(e)
       	{
       	  _callback(this.responseText);
       	};
       	xhr.onerror = function(e)
       	{
       	  _callback('XHR >>> '+JSON.stringify(e));
       	};
       	xhr.open(_method, _url, _sync);
       	xhr.send();
       	// If it's a sync call, you can grab the response text after
       	if(_sync == false) {
       		_callback(xhr.responseText);
       	}
       };
       
  3. Pedro Enrique 2014-09-05

    Documentation: http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Network.HTTPClient-method-send
  4. Pedro Enrique 2014-09-05

    Marking this as invalid since making asynchronous calls on the HTTP Client does not fire the callback, this is by design. The sample in my previous comments should solve the issue.
  5. Ingo Muschenetz 2014-09-08

    This is a behavior change. In ASIHttp, we specifically hacked around the behavior to make a callback on a synchronous call. In NSURLConnection, we decided this was no longer a good idea and stopped it. In general, we strongly recommend you not use synchronous network calls unless there is a good rationale of doing so.
  6. Lee Morris 2017-03-21

    Closing ticket as invalid.

JSON Source