Connecting to a network and retrieving resources is a basic requirement of mobile computing. With a BlackBerry 10 device, you can retrieve the cellular or Wi-Fi network status, check the network status in response to a network status event, and send and receive data over a network.

To accomplish these network-related tasks, BlackBerry 10 includes networking libraries such as libcurl, libsocket, and QtNetwork.

  • The libcurl library provides the functionality to transfer data with URL syntax, using protocols such as FILE, HTTP, IMAP, POP3, FTP, telnet, and many others.
  • The libsocket library provides support for basic sockets and TCP/IP functionality.
  • The QtNetwork library exposes a mix of low-level and high-level APIs used for sending and receiving data over a network.


The QNetworkAccessManager class provides the functionality to send and receive data over a network. The image shown below demonstrates the relationship between the QNetworkAccessManager and the QNetworkRequest and QNetworkReply classes.

A flow diagram showing that data flows from QNetworkRequest to QNetworkAccessManager and then to QNetworkReply.

QNetworkRequest represents an HTTP request. At a minimum, it must contain a URL for the destination. The request can also contain HTTP headers and SSL options.

Required and option attributes of a QNetworkRequest.

QNetworkAccessManager receives network requests and sends them over the network using one of its HTTP request functions (get(), post(), put(), and so on), which returns a pointer to its QNetworkReply. These functions are all asynchronous in nature, so they do not block your main app thread.

QNetworkAccessManager also contains functionality for cookie handling, caching, and proxy handling.

The methods and attributes of a QNetworkAccessManager.

QNetworkReply represents an HTTP reply. Among other information, it contains reply data from the destination URL, HTTP headers consisting of the status code, and any errors. Data in a QNetworkReply is reported asynchronously, so you can connect to its signals and manage information as it arrives.

When new data arrives over the network, the readyRead() signal is emitted to indicate that the data is ready for reading. If you want to parse data as it arrives, you should subscribe to this signal. The readyRead() signal can be used even if you're not finished with the reply. When an HTTP request is complete, the finished() signal is emitted to indicate this event to your app.

The QNetworkReply API.


To use the networking APIs in your app, you need to link to the QtNetwork module by adding the following line to your .pro file:

QT += network

Retrieve network status

When you're developing apps, you might need to know if the device is connected to a network and if that connection is either cellular or Wi-Fi.

Not applicable

You can use the classes in the QtNetwork module to find out if your device is connected to a network.

The following example demonstrates how to retrieve and listen for changes to the online status (online or offline) and the configuration type (WLAN, 2G, 3G, and so on).

// Create a QNetworkConfigurationManager object to manage 
// the network configurations and a QNetworkAccessManager to 
// find the name of the network interface

m_pNetConfigMngr = new QNetworkConfigurationManager();
m_pNetAccessMngr = new QNetworkAccessManager();

// Set up a connection between the 
// QNetworkConfigurationManager's onlineStateChanged() signal 
// to the app's onOnlineStatusChanged(bool) slot. The state 
// indicates whether the device is online or offline

bool res = QObject::connect(m_pNetConfigMngr,


// Set up a connection between the 
// QNetworkConfigurationManager's configurationChanged()signal 
// to the app's onConfigurationChanged() slot. The  
// configuration indicates the type of connection that the  
// device has (WLAN, 2G, 3G, and so on)

res = QObject::connect(m_pNetConfigMngr,
      SIGNAL(configurationChanged(const QNetworkConfiguration &)),


// When the onlineStateChanged() signal is emitted,  
// get the new online status (online or offline)

void ApplicationUI::onOnlineStatusChanged(bool isOnline)
    // Get the 'Online' status from the onlineStateChanged()
    // signal

    QString onlineStatus = isOnline ? "Online" : "Offline";

    // Do something with the online status

// When the configurationChanged() signal is emitted, get the  
// new network interface
void ApplicationUI::onConfigurationChanged()
    // Retrieve the interface (WLAN, 2G, and so on) being used  
    // to make the connection

    QString intrfceNme = 

    // Do something with the new interface name

To see how the online status and network interface information can be incorporated into a UI, see Tutorial: Explore the Networking Downloading a File sample app.

Before calling any functions, you must initialize the BlackBerry Platform Services library by calling bps_initialize(). You can retrieve the current network status at any time by calling netstatus_get_info(), which returns the network status information in a netstatus_info_t structure.

The netstatus_info_t structure contains various network status properties (for example, IPv4 status or default interface). You must use the appropriate netstatus_info_get_*() ( netstatus_info_get_ip4_status(), netstatus_info_get_default_interface(), and so on) to retrieve the properties. For example, you can call netstatus_info_get_ip4_status() with the network status information returned by netstatus_get_info() to retrieve the IPv4 status. Then you can use this status information to adjust your app's operation.

To check the network status in response to a network status event, you must request that network status events be delivered to your app by calling netstatus_request_events(). After your app is registered to receive network status events, you can call bps_get_event() to retrieve the next available event.

For most apps, bps_get_event() is called repeatedly to respond to events in a timely manner. You can use an event loop with a simple exit condition to accomplish this and retrieve events inside this loop.

After an event is retrieved, you should test it to see if it's part of the network status domain by calling bps_event_get_domain() and comparing the result to netstatus_get_domain(). You also need to make sure that the event code is NETSTATUS_INFO, because this is the type of event that's generated when the network status changes. After you verify the event domain and code, retrieve the network status by calling netstatus_get_info() and then respond to the status appropriately.

#include <bps/bps.h>
#include <stdlib.h>
#include <bps/netstatus.h>

/* Create and initialize variables */
int shutdown  = 0;
bool is_available = false;
netstatus_ip_status_t ip4status;
int numgateways = 0;
const char * defaultgateway;
const char * defaultinterface;
int numnameservers = 0;
const char * nameserver;
int numsearchdomains = 0;
const char * searchdomain;
bool loginrequired = false;

int main(int argc, char *argv[]) {

    /* Initialize BPS */

    /* Pass 0 to request all network status events */

    while (!shutdown) {
      bps_event_t *event = NULL;

      /* Pass -1 to have the function block until an event occurs */
      bps_get_event(&event, -1);     

      if (event) {
        if (bps_event_get_domain(event) == netstatus_get_domain()) {
          if (NETSTATUS_INFO == bps_event_get_code(event)) {
            netstatus_info_t *info = netstatus_event_get_info(event);
            if (info) {

              /* Extract information using netstatus_info_get_*()
                 functions */

              is_available = netstatus_info_get_availability(info);
              ip4status = netstatus_info_get_ip4_status(info);
              numgateways = netstatus_info_get_num_default_gateways(info);
              if (numgateways > 0) {
                  /* Get the gateway at index 0 of info */ 
                  defaultgateway = netstatus_info_get_default_gateway(info, 0);
              defaultinterface = netstatus_info_get_default_interface(info);
              numnameservers = netstatus_info_get_num_name_servers(info);

              if (numnameservers > 0){
                  /* Get the name server at index 0 of info */
                  nameserver = netstatus_info_get_name_server(info, 0);
              numsearchdomains = netstatus_info_get_num_search_domains(info);

              if (numsearchdomains > 0){
                  /* Get the search domain at index 0 of info */
                  searchdomain = netstatus_info_get_search_domain(info, 0);

              loginrequired = netstatus_info_get_http_proxy_login_required(info);

              /* Adjust app based on network status information here */
    return 0;

Send an HTTP request

Not applicable

Here's an example that shows you how to send an HTTP GET request and retrieve the network reply. You need to create the network request and set the destination URL as a QUrl object. Then, create the network access manager, connect a custom slot to handle the finished() signal, and send the HTTP GET request.

You can define a variable m_pNetReply for various purposes, such as updating your UI and retrieving the raw headers. You can retrieve the raw headers from the m_pNetReply member variable that is set when the request is made to the server.

When the network reply has finished processing, call deleteLater() to delete the reply object and free up system resources.

// Create the network request and set the destination URL

QNetworkRequest request = QNetworkRequest();
// Create the network access manager 

QNetworkAccessManager* m_pNetAccessMngr = 
                             new QNetworkAccessManager(this);

// Connect your custom slot to its finished() signal
// and check the return value for connection errors

bool res;

res = connect(m_pNetAccessMngr, 

// Use the QNetworkAccessManager object to send the HTTP 
// request and capture the reply

m_pNetReply = m_pNetAccessMngr->get(request);
void ApplicationUI::onRequestFinished ()
    // Check for errors and verify that data is available

    if (m_pNetReply != NULL &&
        m_pNetReply->bytesAvailable() > 0 &&
        m_pNetReply->error() == QNetworkReply::NoError)
        // ...
        // Use the data in the m_pNetReply member variable
        // to update other components in your UI


        // Use the reply to set a raw headers dialog box

        QString rawHdrInfo = getRawHeaderInfo(m_pNetReply);

        // ...
        // Handle errors here
// Collect the raw headers and return them as a QString object

QString ApplicationUI::getRawHeaderInfo ( QNetworkReply* reply )
    QList <QByteArray> rawHeaderList = reply->rawHeaderList();
    QString rawHdrs;

    foreach(QByteArray hdrName, rawHeaderList)
        QByteArray hdrValue = reply->rawHeader(hdrName);
        rawHdrs += hdrName;
        rawHdrs += " : ";
        rawHdrs += hdrValue;
        rawHdrs += "\n";

    return rawHdrs;

Here's an example that uses libcurl functions to perform an HTTP GET request and outputs the response.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

/* Define a struct to store response data */
struct msg_data {
    char *ptr;
    size_t len;

/* Define a function to initialize a msg_data struct */
void init_msg_data(struct msg_data *msg) {
    msg->len = 0;
    msg->ptr = malloc((msg->len)+1);
    if (msg->ptr == NULL)
        {        fprintf(stderr,
        "malloc() failed\n");
    msg->ptr[0] = '\0';

/* Copy the response to stream, allocating memory to store
   the data and updating the size of the stream */
size_t write_response_cb(void *buffer, size_t size, size_t nmemb, struct msg_data *stream)
    size_t new_len = stream->len + (size * nmemb);
    stream->ptr =  realloc(stream->ptr, new_len + 1);
    if (stream->ptr == NULL) {
        fprintf(stderr, "realloc() failed.\n");

    memcpy(stream->ptr + stream->len, buffer, (size * nmemb));
    stream->ptr[new_len] = '\0';
    stream->len = new_len;

    return (size *  nmemb);

int main(int argc, char * argv[])
/* Initialize curl, and create and initialize a variable
   to hold the response */ 
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

/* Configure the options for the GET action. Set the URL
   to send the GET response to, and specify a write callback
   function and a location to write the response data to */
    if (curl) {
        struct msg_data response;
        curl_easy_setopt(curl, CURLOPT_URL, "");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response_cb);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

        /* Perform the GET request. The GET request is sent to
           the specific URL and any response is processed by 
           the callback function, which stores the response
           in response. Output the final response */
        res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        printf("%s\n", response.ptr);
    return 0;

Related resources

Last modified: 2015-05-07

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus