对。好吧,这实际上是一个两部分的问题。一是如何发出请求并接收回复,二是如何解析JSON;幸运的是,Cascades 已经涵盖了这两种情况。
提出请求:
QNetworkAccessManager qNam;
QNetworkRequest req("192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=1");
QNetworkReply *reply = qNam.get(req);
connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
然后像这样定义 onFinished 插槽:
void ClassName::onFinished() {
QNetworkReply *reply = dynamic_cast<QNetworkReply*>(sender());
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
JsonDataAccess jda;
QVariantMap map = jda.loadFromBuffer(reply->readAll()).toMap();
QVariantList addresses = map["address"].toList();
foreach(QVariant var, addresses) {
QVariantMap addressMap = var.toMap();
qDebug() << "Area is " << addressMap["area"].toString();
qDebug() << "City is " << addressMap["city"].toString();
}
}
else {
qDebug() << "Server returned code " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
}
}
为此,该方法必须在类中标记为Q_SLOT
.