-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhttpengine.cpp
112 lines (104 loc) · 4.13 KB
/
myhttpengine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//SOURCE ADAPTED FROM http://www.developer.nokia.com/Community/Wiki/Creating_an_HTTP_network_request_in_Qt
//AND http://www.developer.nokia.com/Community/Wiki/QXmlStreamReader_to_parse_XML_in_Qt
#include "myhttpengine.h"
MyHttpEngine::MyHttpEngine(QQmlContext * myContext, QObject *parent) :
QObject(parent)
{
this->myContext = myContext;
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
QUrl url("http://api.musescore.com/services/rest/score.xml&oauth_consumer_key=musichackday&part=0,73&sort=view_count");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
// NOTE: Store QNetworkReply pointer (maybe into caller).
// When this HTTP request is finished you will receive this same
// QNetworkReply as response parameter.
// By the QNetworkReply pointer you can identify request and response.
}
void MyHttpEngine::finishedSlot(QNetworkReply* reply)
{
// Reading attributes of the reply
// e.g. the HTTP status code
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (reply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
QByteArray result = reply->readAll(); // bytes
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(result);
QList< QMap<QString,QString> > scores;
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() && !xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) {
continue;
}
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
/* If it's named persons, we'll go to the next.*/
if(xml.name() == "scores") {
continue;
}
/* If it's named person, we'll dig the information from there.*/
if(xml.name() == "score") {
this->parseScore(xml);
}
}
}
}
// Some http error received
else
{
// handle errors here
}
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete reply;
myContext->setContextProperty("listModelC",QVariant::fromValue(scoreList));
}
void MyHttpEngine::parseScore(QXmlStreamReader& xml) {
QString name;
QString description;
QString id;
/* Let's check that we're really getting a score. */
if(xml.tokenType() != QXmlStreamReader::StartElement &&
xml.name() == "score") {
return;
}
/* Let's get the attributes for person */
QXmlStreamAttributes attributes = xml.attributes();
/* Next element... */
xml.readNext();
/*
* We're going to loop over the things because the order might change.
* We'll continue the loop until we hit an EndElement named person.
*/
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "score")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
/* We've found title. */
if(xml.name() == "title") {
xml.readNext();
name = xml.text().toString();
}
/* We've found description. */
if(xml.name() == "genre") {
xml.readNext();
description = xml.text().toString();
}
/* We've found id. */
if(xml.name() == "id") {
xml.readNext();
id = xml.text().toString();
}
}
/* ...and next... */
xml.readNext();
}
scoreList.append(new ScoreInfo(name, description, id));
}