发送HTTP发布请求BlackBerry 10

时间:2013-02-05 08:14:16

标签: c++ blackberry-10

我正在基于C ++在BB 10上开发一个应用程序,我需要向服务器发送HTTP post请求并检索一些JSON数据。是否有一些框架类可以帮助您向服务器发送HTTP post请求?任何代码链接等?感谢。

1 个答案:

答案 0 :(得分:2)

我不记得我发现这个代码的示例应用程序,但这适用于我在我的应用程序中所需的内容。

在c ++中添加以下内容

头文件(.hpp)

public:
    Q_INVOKABLE void doNetworkRequest(QString url);

signals:
void networkReply(const QVariantMap &data);
void networkError();

private Q_SLOTS:
    void handleNetworkData(QNetworkReply *reply);

private:
    QNetworkAccessManager networkManager;

然后在主文件(.cpp)中添加它

这是主要的应用程序功能

// Hook this signal so we can respond to network replies
connect(&networkManager, SIGNAL(finished(QNetworkReply *)), this,
        SLOT(handleNetworkData(QNetworkReply *)));

添加以下功能:

void Top12Wines::doNetworkRequest(QString url)
{
qDebug() << "Request URL " << url;
QUrl qurl = url;
networkManager.get(QNetworkRequest(qurl));
}

void Top12Wines::handleNetworkData(QNetworkReply *reply)
{

if (!reply->error()) {
    qDebug() << "Got network data";
    // Let's get ALL the data
    const QByteArray response(reply->readAll());

    JsonDataAccess jda;
    QVariantMap results = jda.loadFromBuffer(response).toMap();

    emit networkReply(results);

} else {
    qDebug() << "Got network error";
    emit networkError();
}

// Cleanup
reply->deleteLater();
}

然后在您的QML中,您可以像这样访问它:

    _App.networkReply.connect(checkVersion); //
    _App.networkError.connect(checkVersionError);
    _App.doNetworkRequest("http://myserver/version.json");

function checkVersion(data)
{
    _App.networkReply.disconnect(checkVersion); //disconnect links after retrieving data
    _App.networkError.disconnect(checkVersionError);
    var newVersion = data.version;
}

function checkVersionError()
{
    _App.networkReply.disconnect(checkVersion); //disconnect links after retrieving data
    _App.networkError.disconnect(checkVersionError);        
    //do something to alert user that an error occurred.
}