×
Namespaces

Variants
Actions

XMLHttpRequest in QML with run time proxy settings

From Nokia Developer Wiki
Jump to: navigation, search

I have modified the exiting networkaccessmanagerfactory example to add the proxy setting dynamically. The example is very easy and will help to resolve lots of XMLHttpRequest queries in QML.

Article Metadata
Code Example
Installation file: none
Tested with
SDK: Qt SDK 1.1.4
Devices(s): Emulator
Compatibility
Platform(s): Symbian, Meego
Device(s): Symbian and Meego
Dependencies: None
Platform Security
Signing Required: Self
Capabilities: NetworkServices
Article
Keywords: XMLHttpRequest, Proxy, QNetworkProxyQuery, QNetworkProxyFactory
Created: kkrish (27 Feb 2012)
Last edited: hamishwillee (30 May 2013)

Introduction

Code snippets to make a async call using XMLHttpRequest.

    function getFrontPage(){
console.debug("getFrontPage IN")
 
var xmlhttp = new XMLHttpRequest();
 
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
console.debug("got the page asynchornously " + xmlhttp.responseText.substring(1, 1000))
} else {
console.log("responseText", xmlhttp.responseText);
}
}
}
 
xmlhttp.open("GET", "http://www.google.com");
xmlhttp.send();
 
console.debug("getFrontPage OUT")
}

Some time the code may not work because you network might be behind the proxy. So now you may need to set the proxy to get the response. You can hard code the proxy settings but what if you change the network? So you can set the proxy dynamically inside the code.

QNetworkProxyQuery npq(QUrl("http://www.google.com"));
QList<QNetworkProxy> proxyList = QNetworkProxyFactory::systemProxyForQuery(npq);
qDebug() << "proxyList[0].hostName() : " << proxyList[0].hostName() + " : proxyList[0].port() : " <<proxyList[0].port() << " : proxyList.count : " << proxyList.count();

I just print the 0th element from the list. You may also make a loop to print all the available proxy.

Now set these proxy details to QNetworkAccessManager. For details info see the attached example.


Example

http://www.developer.nokia.com/Community/Wiki/File:Networkaccessmanagerfactory.zip

Other Links

http://www.developer.nokia.com/Community/Wiki/Why_does_QML_require_QDeclarativeNetworkAccessManagerFactory

This page was last modified on 30 May 2013, at 06:34.
65 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×