Android ( Emulator too ) and XMLHttpRequest
-
wrote on 28 Nov 2013, 13:58 last edited by
I have problem to get response from youtube in android.
From main.qml file making request like this :
@onTrailerUrlChanged: {
console.log('loading youtube link');
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE)
{
console.log('Youtube link loaded: '+doc.responseText)
var jsresp = JSON.parse(doc.responseText);
var entries = jsresp.feed.entry;
var len = entries.length;
if (len) {
var entry = entries[0];
var thumbnail_url = entry.media$group.media$thumbnail[0].url;
var video_3gp_url = entry.media$group.media$content[1].url;
console.log('Thumbnail link '+thumbnail_url);
console.log('Video url '+video_3gp_url);
if (thumbnail_url)
thumbnail_image.source = thumbnail_url;
if (video_3gp_url)
trailerRtpsLink = video_3gp_url;
}
}
}var video_id = trailerUrl.replace('http://www.youtube.com/watch?v=',''); console.log('link to parse youtube '+'http://gdata.youtube.com/feeds/api/videos?q='+video_id+'&alt=json&fields=entry/id,entry/media:group/media:thumbnail,entry/media:group/media:content') doc.open("GET",'https://gdata.youtube.com/feeds/api/videos?q='+video_id+'&alt=json&fields=entry/id,entry/media:group/media:thumbnail,entry/media:group/media:content'); doc.send(); }@
And doc.responseText is empty. But on desktop everthing is working preoprly. I have read that making network request from main thread in android is not supported. Then if qml running in main thread how make request from qml and put in other thread. Is this possible ?
-
you should also check if an error has occured once your request has finished, maybe this gives already an clue whats going wrong.
@
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE)
{
if( doc.status != 200 )
{
console.log('ERROR: '+doc.status);
}
console.log('Youtube link loaded: '+doc.responseText)
var jsresp = JSON.parse(doc.responseText);
var entries = jsresp.feed.entry;
var len = entries.length;
if (len) {
var entry = entries[0];
var thumbnail_url = entry.media$group.media$thumbnail[0].url;
var video_3gp_url = entry.media$group.media$content[1].url;
console.log('Thumbnail link '+thumbnail_url);
console.log('Video url '+video_3gp_url);
if (thumbnail_url)
thumbnail_image.source = thumbnail_url;
if (video_3gp_url)
trailerRtpsLink = video_3gp_url;
}
}
}
@Maybe you forgot to add the permissions to your android app to access the internet?
1/2