BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 3: Networking

Native SDK Development

Networking in BlackBerry 7 OS was based on the Connector.open() paradigm, where you would specify the protocol and transport in the connection string (“http://www.example.com;deviceside=true”), and get back a Connection object of a certain type (HttpConnection, for example).

Networking in BlackBerry 10 is done through the QNetwork module. You no longer need to worry about transport selection. Your typical HTTP communication can be handled through QNetworkAccessManager. It provides methods such as get() and post(), which take a QNetworkRequest and then asynchronously return a QNetworkReply.

For example:

QUrl *url = new QUrl("http://www.iana.org/domains/example/");

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);

    connect(manager, SIGNAL(finished(QNetworkReply*)),
    		this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(*url));

And then in your replyFinished slot:

void App::replyFinished(QNetworkReply* reply) {
	qDebug() <readAll();

	// Call deleteLater to make sure the reply gets cleaned up
	reply->deleteLater();
}

You can also do TCP with QTCPSocket and QTCPServer, UDP with UDPSocket , and FTP with QFtp.

And of course, lower level libraries such as cURL are available if you should require them.

About Paul Bernhardt

I work for BlackBerry on the Developer relations team helping developers write apps in Cascades. I focus on camera, gaming and Unity mostly. My views are my own. In my free time, I'm a hardcore PC gamer and draw D&D maps for fun!

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus