Search code examples
c++qtblackberryblackberry-10blackberry-cascades

Blackberry 10: How to correspond incoming http replies to their previous http request counterparts?


First off, I would like to say I'm totally new to BB and I'm coming from an Android background.

I've been looking at samples such as:

https://developer.blackberry.com/cascades/documentation/device_comm/networking/

I have an application which is making a lot of different (and similar) web requests. How do I identify these incoming replies so I can demux them to their appropriate components? Can I tag them somehow?

Thanks and please let me know if I can be more clear.


Solution

  • As @Kernald wrote above, all the information you're likely requesting could be found in QNetworkReply object. You get pointer to this object after placing a request by calling QNetworkAccessManager::get() or QNetworkAccessManager::put()

    When you get the reply it's delivered via QNetworkAccessManager::finished(QNetworkReply *reply) signal

    Here you can get access to the counterparts Via pointer to respective QNetworkRequestand it's contents depending on what you're after

    QNetworkAccessManager* networkAccessManager;
    
    // skipped
    
    bool result = connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    Q_ASSERT(result);
    
    // skipped 
    
    void requestFinished(QNetworkReply* reply) {
        QNetworkRequest* request = reply->request();
        QUrl url = request->url(); // get the URL
        QVariant header = request->header(); // get the header
        // etc...
    }
    

    Also, you're able to get raw headers of network reply like that:

    QByteArray hdr;
    QList<QByteArray> list = reply->rawHeaderList();
    Q_FOREACH(hdr, list){
            qDebug() << hdr;
    }
    

    If it's not enough for some reason, you might manually tag network request by assigning an QNetworkRequest::Attribute to QNetworkRequest object:

    QNetworkRequest request; // Create and send the network request
    QNetworkRequest::Attribute attr = QNetworkRequest::User+1; // any unique value greater than QNetworkRequest::User
    QString myStuff;
    request.setAttribute(attr, myStuff);
    

    These attribute values must be greater that QNetworkRequest::User up to QNetworkRequest::UserMax. Afterwards you get the attribute previously assigned to the request in the following way:

    void requestFinished(QNetworkReply* reply) {
        QNetworkRequest* request = reply->request();
        QNetworkRequest::Attribute myAttr = QNetworkRequest::User+1;
        QVariant myStuff = reply->request().attribute(myAttr);
        // do something further
    }
    

    Here is official BB10 and Qt (for the version 4.8 which is currently used at the latest Blackberry 10 SDK) documentation on this: