[TIMOB-13031] Android: Allow HTTPClient.setRequestHeader() to be called after open()
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Low |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2013-03-20T22:56:46.000+0000 |
| Affected Version/s | Release 3.0.2 |
| Fix Version/s | Release 3.1.0, 2013 Sprint 06 API, 2013 Sprint 06 |
| Components | Android |
| Labels | android, exalture, mobilesdk, qe-devCheck, titanium |
| Reporter | Ashish Nigam |
| Assignee | Sunila |
| Created | 2013-03-12T06:50:31.000+0000 |
| Updated | 2013-04-01T23:42:51.000+0000 |
Description
Using HTTP client with Titanium produces different behavior.
Titanium.UI.setBackgroundColor('#000');
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Window 1',
backgroundColor:'#000'
});
var videoID = 'AO2GwKAfEqY';
vdrld = function(a, b) {// We do not get the video-id nor do we get any url; that is, we extract the ID from the image-url.
vdldr = Ti.Network.createHTTPClient();
vdldr.onload = function() {
var x, y;
Ti.API.error("################# onLoad ## ###################" + " "+ this.responseText);
x = decodeURIComponent(decodeURIComponent(decodeURIComponent(decodeURIComponent(this.responseText.substring(4, this.responseText.length)))));
Ti.API.debug('vdrld: Resulting JSON was: ' + JSON.stringify(x));
y = JSON.parse(x).content.video["fmt_stream_map"][0].url;
//Ti.API.error(y);
alert(y);
//Ti.API.error(x);
alert(x);
if (y === undefined) {
Ti.API.error("vdrld: Found no fmt_stream_map in the returned JSON!!!");
return;
}
return b(y);
};
vdldr.setRequestHeader("Referer", "http://www.youtube.com/watch?v=" + a);
vdldr.setRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14");
vdldr.open("GET", "http://m.youtube.com/watch?ajax=1&feature=related&layout=mobile&tsp=1&&v=" + a);
vdldr.send()
};
setTimeout(function() {
vdrld(videoID, function() {
Ti.API.debug('The resulting URL from vdrld was: ' + arguments[0]);
var videoPlayerParms = {
fullscreen: false,
top: 5,
width: 320,
height: 240,
borderColor: '#aaa',
autoplay: true,
mediaControlStyle: Titanium.Media.VIDEO_CONTROL_EMBEDDED,
scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT,
url: arguments[0],
};
var videoPlayer = Ti.Media.createVideoPlayer(videoPlayerParms);
win1.add(videoPlayer);
});
}, 5000);
win1.open();
*Above is working in iOS but giving error in android*
*Error Message: setRequest Header can only be called before invoking send*
it seems like behavior is not similar in iOS and Android, as i change the order of setRequestHeader method and open method then it works on both but if i try the same code then it does not show error in iOS but in Android it produces the error message mentioned above.
Here is the patch to fix this:
diff --git a/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java b/android/modules/network/src/java/ti/modules/titanium/network/TiHT index 372a2c5..793924d 100644 --- a/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java +++ b/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java @@ -736,7 +736,7 @@ public class TiHTTPClient public void setRequestHeader(String header, String value) { - if (readyState == READY_STATE_OPENED) { + if (readyState <= READY_STATE_OPENED) { headers.put(header, value); } else {https://github.com/appcelerator/titanium_mobile/pull/3962
Verified fix with mobile sdk-3.1.0.v20130401102449 Titanium Studio, build: 3.1.0.201304010847 Device: HTC Amaze, Android 4.0.3 There was this in the logs:
However, since the issue was doing setRequestHeader, and that appears to work, this worked, I guess.E/TiAPI (10272): ################# onLoad ## ################### )]}'{"build_id": 1364794790, "build_signature": "en:900821,900823,900824,901802,902000,906383,906836,908529,910223,915101,919373,919512,920201,923019,926400,929602,929903,930101,930603,930803,930807,931202,931203,931401,932000", "signed_in_username": "", "result": "ok", "content": {"video": {"public_name": "UMHealthSystem", "longform": false, "hq_stream_url": "", "length_seconds": 151, "encrypted_id": "AO2GwKAfEqY", "description": "Dr. Michael Sabel from the University of Michigan Multidisciplinary Melanoma Clinic talks about melanoma and the importance of skin cancer self-exam especially for those who are at a high risk for skin cancer. Dr. Sabel introduces the University of Michigan's new Mobile App: UMSkinCheck which helps you complete a self-exam. For more information visit: www.uofmhealth.org\/umskincheck", "watch_link": "\/watch?v=AO2GwKAfEqY", "player_type": "rtsp", "playability": "PLAY_OK", "profile_url": "\/user\/UMHealthSystem", "thumbnail_for_watch": "http:\/\/i.ytimg.com\/vi\/AO2GwKAfEqY\/hqdefault.jpg?w=320\u0026h=192\u0026sigh=GSHLS-pOgF89vQ6_hl86j1QeTf0", "likes_num": 3, "stream_url": "rtsp:\/\/v3.cache5.c.youtube.com\/CjYLENy73wIaLQmmEh-gwIbtABMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJHYrZSDxICtUQw=\/0\/0\/0\/video.3gp", "comment_count": 4, "dislikes_num": 1, "thumbnail_info": null, "title": "Introducing UMSkinCheck, a Mobile Skin Check App from the University of Michigan", "duration": "2:31", "view_count": "2,620", "time_created_text": "May 17, 2012", "user_image_url": "\/\/i3.ytimg.com\/i\/fp5MBK2IC90Bdm5IQUJvYw\/1.jpg?v=712528"}, "next_url": "\/results?search=related\u0026q=\u0026p=2\u0026ps=12\u0026v=AO2GwKAfEqY", "related_videos": [{"public_name": "SkinDiseaseEducation", "encrypted_id": "8PjWDz4FVgM", "watch_link": "\/watch?feature=fvwrel\u0026v=8PjWDz4FVgM", "playability": "NO_PREFETCH", "thumbnail_for_watch": "http:\/\/i.ytimg.com\/vi\/8PjWDz4FVgM\/hqdefault.jpg?w=320\u0026h=192\u0026sigh=yfo4NxjHGGpLeArFmipdSjI_uOI", "related_feature": "fvwrel", "thumbnail_info": {"thumb_width": 120, "url": "http:\/\/i.ytimg.com\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,--EpUAsphkw,fDwigwLwd5A,QEugrDsfj3E,TdSToUqzZ3E,CEZYXcrvvAA,Y-FPQiYXTmM\u0026w=120\u0026h=72\u0026sigh=K8fdpb_I8txlQnhAKRh76g7Qok0", "thumb_height": 72, "height": 1040, "stitched": 1, "width": 128, "posx": 0, "posy": 0}, "title": "Skin Cancer Cutaneous T-cell Lymphoma Treatment", "duration": "2:51", "view_count": "2,916"}, {"public_name": "UMHealthSystem", "encrypted_id": "L7e4app0Azo", "watch_link": "\/watch?feature=relmfu\u0026v=L7e4app0Azo", "playability": "NO_PREFETCH", "thumbnail_for_watch": "http:\/\/i.ytimg.com\/vi\/L7e4app0Azo\/hqdefault.jpg?w=320\u0026h=192\u0026sigh=u7b6vYzegq-jVZSF9ltp8AFRWbY", "related_feature": "relmfu", "thumbnail_info": {"thumb_width": 120, "url": "http:\/\/i.ytimg.com\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,--EpUAsphkw,fDwigwLwd5A,QEugrDsfj3E,TdSToUqzZ3E,CEZYXcrvvAA,Y-FPQiYXTmM\u0026w=120\u0026h=72\u0026sigh=K8fdpb_I8txlQnhAKRh76g7Qok0", "thumb_height": 72, "height": 1040, "stitched": 1, "width": 128, "posx": 0, "posy": 80}, "title": "Match Day 2013", "duration": "2:41", "view_count": "4,219"}, {"public_name": "UMHealthSystem", "encrypted_id": "0w2Lo_nYRPs", "watch_link": "\/watch?feature=relmfu\u0026v=0w2Lo_nYRPs", "playability": "NO_PREFETCH", "thumbnail_for_watch": "http:\/\/i.ytimg.com\/vi\/0w2Lo_nYRPs\/hqdefault.jpg?w=320\u0026h=192\u0026sigh=-pq48K-Vkbjd48VQ49gszuOlogY", "related_feature": "relmfu", "thumbnail_info": {"thumb_width": 120, "url": "http:\/\/i.ytimg.com\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,--EpUAsphkw,fDwigwLwd5A,QEugrDsfj3E,TdSToUqzZ3E,CEZYXcrvvAA,Y-FPQiYXTmM\u0026w=120\u0026h=72\u0026sigh=K8fdpb_I8txlQnhAKRh76g7Qok0", "thumb_height": 72, "height": 1040, "stitched": 1, "width": 128, "posx": 0, "posy": 160}, "title": "Lung Transplant Education", "duration": "59 D/TiAPI (10272): vdrld: Resulting JSON was: "{\"build_id\": 1364794790, \"build_signature\": \"en:900821,900823,900824,901802,902000,906383,906836,908529,910223,915101,919373,919512,920201,923019,926400,929602,929903,930101,930603,930803,930807,931202,931203,931401,932000\", \"signed_in_username\": \"\", \"result\": \"ok\", \"content\": {\"video\": {\"public_name\": \"UMHealthSystem\", \"longform\": false, \"hq_stream_url\": \"\", \"length_seconds\": 151, \"encrypted_id\": \"AO2GwKAfEqY\", \"description\": \"Dr. Michael Sabel from the University of Michigan Multidisciplinary Melanoma Clinic talks about melanoma and the importance of skin cancer self-exam especially for those who are at a high risk for skin cancer. Dr. Sabel introduces the University of Michigan's new Mobile App: UMSkinCheck which helps you complete a self-exam. For more information visit: www.uofmhealth.org\\/umskincheck\", \"watch_link\": \"\\/watch?v=AO2GwKAfEqY\", \"player_type\": \"rtsp\", \"playability\": \"PLAY_OK\", \"profile_url\": \"\\/user\\/UMHealthSystem\", \"thumbnail_for_watch\": \"http:\\/\\/i.ytimg.com\\/vi\\/AO2GwKAfEqY\\/hqdefault.jpg?w=320\\u0026h=192\\u0026sigh=GSHLS-pOgF89vQ6_hl86j1QeTf0\", \"likes_num\": 3, \"stream_url\": \"rtsp:\\/\\/v3.cache5.c.youtube.com\\/CjYLENy73wIaLQmmEh-gwIbtABMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJHYrZSDxICtUQw=\\/0\\/0\\/0\\/video.3gp\", \"comment_count\": 4, \"dislikes_num\": 1, \"thumbnail_info\": null, \"title\": \"Introducing UMSkinCheck, a Mobile Skin Check App from the University of Michigan\", \"duration\": \"2:31\", \"view_count\": \"2,620\", \"time_created_text\": \"May 17, 2012\", \"user_image_url\": \"\\/\\/i3.ytimg.com\\/i\\/fp5MBK2IC90Bdm5IQUJvYw\\/1.jpg?v=712528\"}, \"next_url\": \"\\/results?search=related\\u0026q=\\u0026p=2\\u0026ps=12\\u0026v=AO2GwKAfEqY\", \"related_videos\": [{\"public_name\": \"SkinDiseaseEducation\", \"encrypted_id\": \"8PjWDz4FVgM\", \"watch_link\": \"\\/watch?feature=fvwrel\\u0026v=8PjWDz4FVgM\", \"playability\": \"NO_PREFETCH\", \"thumbnail_for_watch\": \"http:\\/\\/i.ytimg.com\\/vi\\/8PjWDz4FVgM\\/hqdefault.jpg?w=320\\u0026h=192\\u0026sigh=yfo4NxjHGGpLeArFmipdSjI_uOI\", \"related_feature\": \"fvwrel\", \"thumbnail_info\": {\"thumb_width\": 120, \"url\": \"http:\\/\\/i.ytimg.com\\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,--EpUAsphkw,fDwigwLwd5A,QEugrDsfj3E,TdSToUqzZ3E,CEZYXcrvvAA,Y-FPQiYXTmM\\u0026w=120\\u0026h=72\\u0026sigh=K8fdpb_I8txlQnhAKRh76g7Qok0\", \"thumb_height\": 72, \"height\": 1040, \"stitched\": 1, \"width\": 128, \"posx\": 0, \"posy\": 0}, \"title\": \"Skin Cancer Cutaneous T-cell Lymphoma Treatment\", \"duration\": \"2:51\", \"view_count\": \"2,916\"}, {\"public_name\": \"UMHealthSystem\", \"encrypted_id\": \"L7e4app0Azo\", \"watch_link\": \"\\/watch?feature=relmfu\\u0026v=L7e4app0Azo\", \"playability\": \"NO_PREFETCH\", \"thumbnail_for_watch\": \"http:\\/\\/i.ytimg.com\\/vi\\/L7e4app0Azo\\/hqdefault.jpg?w=320\\u0026h=192\\u0026sigh=u7b6vYzegq-jVZSF9ltp8AFRWbY\", \"related_feature\": \"relmfu\", \"thumbnail_info\": {\"thumb_width\": 120, \"url\": \"http:\\/\\/i.ytimg.com\\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,--EpUAsphkw,fDwigwLwd5A,QEugrDsfj3E,TdSToUqzZ3E,CEZYXcrvvAA,Y-FPQiYXTmM\\u0026w=120\\u0026h=72\\u0026sigh=K8fdpb_I8txlQnhAKRh76g7Qok0\", \"thumb_height\": 72, \"height\": 1040, \"stitched\": 1, \"width\": 128, \"posx\": 0, \"posy\": 80}, \"title\": \"Match Day 2013\", \"duration\": \"2:41\", \"view_count\": \"4,219\"}, {\"public_name\": \"UMHealthSystem\", \"encrypted_id\": \"0w2Lo_nYRPs\", \"watch_link\": \"\\/watch?feature=relmfu\\u0026v=0w2Lo_nYRPs\", \"playability\": \"NO_PREFETCH\", \"thumbnail_for_watch\": \"http:\\/\\/i.ytimg.com\\/vi\\/0w2Lo_nYRPs\\/hqdefault.jpg?w=320\\u0026h=192\\u0026sigh=-pq48K-Vkbjd48VQ49gszuOlogY\", \"related_feature\": \"relmfu\", \"thumbnail_info\": {\"thumb_width\": 120, \"url\": \"http:\\/\\/i.ytimg.com\\/vt?cids=8PjWDz4FVgM,L7e4app0Azo,0w2Lo_nYRPs,1xVsO9MyRC4,CtdLOvju_FU,D6mHxyLguhQ,ZHHdAaV-P10,- E/V8Exception(10272): Exception occurred at app.js:22: Uncaught TypeError: Cannot read property '0' of undefined E/XMLModule(10272): (KrollRuntimeThread) [7447,7447] Error parsing XML E/XMLModule(10272): org.xml.sax.SAXParseException: Unexpected token (position:TEXT )]}'{"build_id":...@1:12497 in java.io.InputStreamReader@40ed0eb8) E/XMLModule(10272): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146) E/XMLModule(10272): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107) E/XMLModule(10272): at ti.modules.titanium.xml.XMLModule.parse(XMLModule.java:82) E/XMLModule(10272): at ti.modules.titanium.network.TiHTTPClient.getResponseXML(TiHTTPClient.java:647) E/XMLModule(10272): at ti.modules.titanium.network.HTTPClientProxy.getResponseXML(HTTPClientProxy.java:93) E/XMLModule(10272): at org.appcelerator.kroll.runtime.v8.V8Object.nativeCallProperty(Native Method) E/XMLModule(10272): at org.appcelerator.kroll.runtime.v8.V8Object.callProperty(V8Object.java:69) E/XMLModule(10272): at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1057) E/XMLModule(10272): at android.os.Handler.dispatchMessage(Handler.java:95) E/XMLModule(10272): at android.os.Looper.loop(Looper.java:154) E/XMLModule(10272): at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112) E/TiHttpClient(10272): (KrollRuntimeThread) [22,7469] Error parsing XML E/TiHttpClient(10272): org.xml.sax.SAXParseException: Unexpected token (position:TEXT )]}'{"build_id":...@1:12497 in java.io.InputStreamReader@40ed0eb8) E/TiHttpClient(10272): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146) E/TiHttpClient(10272): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107) E/TiHttpClient(10272): at ti.modules.titanium.xml.XMLModule.parse(XMLModule.java:82) E/TiHttpClient(10272): at ti.modules.titanium.network.TiHTTPClient.getResponseXML(TiHTTPClient.java:647) E/TiHttpClient(10272): at ti.modules.titanium.network.HTTPClientProxy.getResponseXML(HTTPClientProxy.java:93) E/TiHttpClient(10272): at org.appcelerator.kroll.runtime.v8.V8Object.nativeCallProperty(Native Method) E/TiHttpClient(10272): at org.appcelerator.kroll.runtime.v8.V8Object.callProperty(V8Object.java:69) E/TiHttpClient(10272): at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1057) E/TiHttpClient(10272): at android.os.Handler.dispatchMessage(Handler.java:95) E/TiHttpClient(10272): at android.os.Looper.loop(Looper.java:154) E/TiHttpClient(10272): at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)Did not complain about request headers.