Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
markwilcox
Posts: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha

You CAN use XMLHttpRequest from QML in Cascades!

Passing data back and forth between QML and C++ is (relatively) quite a lot of work and complicates apps.  A lot of the performance benefit is lost in converting data types (particularly JSON -> QVariant<List/Map> structures -> JavaScript Objects is a bit crazy if you ask me). If you don't need to do much processing of the data then simply accessing the network from JavaScript is much more convenient.  If you're accessing a webservice that uses JSON then it's ideal.

 

Looking at this thread:

http://supportforums.blackberry.com/t5/Cascades-Development/Method-for-making-an-HTTP-style-request-...

With developers claiming this doesn't work and an official BB dev advisor answer with a C++ mixing example, plus total lack of QML only samples I was wondering if this was really broken.

 

I know it worked fine for others in QtQuick with standard Qt, so it was worth a go trying in Cascades.  Took me a couple of hours to get it working due to the lack of a debugger or useful error messages from QML (plus I'd not used the web service before) so I thought I'd share some skeleton code:

function submitPost(postString) {
                var request = new XMLHttpRequest();
                request.onreadystatechange=function() {
                    // Need to wait for the DONE state or you'll get errors
                    if(request.readyState === XMLHttpRequest.DONE) {
                        if (request.status === 200) {
                            console.log("Response = " + request.responseText);
                            // if response is JSON you can parse it
                            var response = JSON.parse(request.responseText);
                            // then do something with it here
                      
                        }
                        else {
                            // This is very handy for finding out why your web service won't talk to you
                            console.log("Status: " + request.status + ", Status Text: " + request.statusText);
                        }
                    }
                }
                // Make sure whatever you post is URI encoded
                var encodedString = encodeURIComponent(postString);
                // This is for a POST request but GET etc. work fine too
                request.open("POST", "https://<your_service_endpoint_here>", true); // only async supported
                // You might not need an auth header, or might need to modify - check web service docs
                request.setRequestHeader("Authorization", "Bearer " + yourAccessToken);
                // Post types other than forms should work fine too but I've not tried
                request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                // Form data is web service dependent - check parameter format
                var requestString = "text=" + encodedString;
                request.send(requestString);
            }

 

While researching this I discovered that *apparently* adding elements to a ListModel in JavaScript is very slow compared to C++, so you don't want to do it with very large numbers of objects.  That said, there are pure QML/JS Twitter clients with decent performance on slow old Symbian devices.  I can't confirm how slow because:

"Currently, you can use the profiler only for C++ code. You can't use the profiler for QML or JavaScript code." :smileysad:

 

If you've only got fairly small amounts of data to pass around, particularly if it's JSON, then I think this is going to be MUCH easier with minimal performance impact.  Being able to do this kind of simple web-connected app entirely in QML was part of the original vision for the language.  Have fun playing with it anyway.

 

Mark

 

Please use plain text.
Developer
simon_hain
Posts: 16,136
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: You CAN use XMLHttpRequest from QML in Cascades!

i would suggest that you convert that post into a knowledge base article, attach the sample project and make everybody a little bit happier.
much better to find later on, and easier to support, as you cannot edit posts here after a certain time.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
markwilcox
Posts: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha

Re: You CAN use XMLHttpRequest from QML in Cascades!

Good suggestion, I'm new to the BB community and really only doing some evaluation of the environment, not actually building an app for submission - I do like being community-spirited though so I looked into it. Seems I have to register to become a "Developer" rather than "Contributor" then submit a draft for a 10 day review period... too much trouble I'm afraid, what's wrong with a good old-fashioned developer wiki. :smileyhappy:

If anyone else would like to verify this works for them and post a knowledge base article borrowing any of my text I will not be at all offended that it has been "stolen".
Please use plain text.
Developer
simon_hain
Posts: 16,136
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: You CAN use XMLHttpRequest from QML in Cascades!

don't worry, it's very easy.
first step: post in the "i want to be a developer" post, and your status will be changed to developer, which has only upsides at the moment.

the review period was much much shorter when i posted my KB article, it is just to ensure that no nonsense is posted, i guess.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
markwilcox
Posts: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha

Re: You CAN use XMLHttpRequest from QML in Cascades!

OK, I'll give it a go. :smileyhappy:
Please use plain text.
Developer
AlexXF
Posts: 323
Registered: ‎03-10-2010
My Device: Bold 9780, DevAlpha

Re: You CAN use XMLHttpRequest from QML in Cascades!

Great!!! I like that way.

Can you add example how to set up cookie / session variable?
Please use plain text.