在blackberry 10级联上创建一个http通信

时间:2013-05-06 13:44:50

标签: c++ cascade blackberry-10

我是开发黑莓10级联的新手。我需要使用httpget连接文件xml并从中获取dat以在列表中显示它。 有一个例子可以帮助我进行http通信或tutoriel? 像https://developer.blackberry.com/cascades/documentation/device_platform/networking/tutorial_http_comm.html这样的http通信的所有链接都不起作用我得到了404

2 个答案:

答案 0 :(得分:4)

使用QNetworkAccessManager,QNetworkRequest和QNetworkReply类进行http连接。

QNetworkAccessManager* netManager = new QNetworkAccessManager();
QUrl myurl(yourURL);
QNetworkRequest req(url);

QNetworkReply* ipReply = netManager->get(req);
connect(ipReply, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReply(QNetworkReply*)));
}

在onReply插槽中解析您的回复

if (reply) {
    if (reply->error() == QNetworkReply::NoError) {
        int available = reply->bytesAvailable();
        if (available > 0) {
            int bufSize = sizeof(char) * available + sizeof(char);
            QByteArray buffer(bufSize, 0);
            int read = reply->read(buffer.data(), available);
            response = QString(buffer);
        }
    } else {
        response =
                QString("Error: ") + reply->errorString()
                        + QString(" status:")
                        + reply->attribute(
                                QNetworkRequest::HttpStatusCodeAttribute).toString();
    }
    reply->deleteLater();
}

访问this page了解详情

答案 1 :(得分:1)

使用此代码...

QNetworkAccessManager* netManager = new QNetworkAccessManager();
        QUrl myurl("http://******");
        QNetworkRequest req(myurl);

    QNetworkReply* ipReply = netManager->get(req);

    QEventLoop eventLoop;
    QObject::connect(ipReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    std::cout << "finished" << std::endl; //request finished here
    requestFinished(ipReply);