0

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

4

2 回答 2

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();
}

访问此页面了解更多信息

于 2013-05-07T04:28:06.400 回答
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);
于 2013-06-25T04:51:10.350 回答