-
Notifications
You must be signed in to change notification settings - Fork 22
/
getbaiduchannel.cpp
56 lines (39 loc) · 1.47 KB
/
getbaiduchannel.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
#include "getbaiduchannel.h"
const QString channelUrl = "http://fm.baidu.com/dev/api/?tn=channellist";
GetBaiduChannel::GetBaiduChannel(QFrame *parent, QNetworkCookieJar *cookie) : QFrame(parent)
{
m_cookJar = cookie;
}
QList<CHANNEL_INFO> GetBaiduChannel::getMusicChannel()
{
QList<CHANNEL_INFO> channelInfoList;
QNetworkRequest networkRequest;
QNetworkAccessManager *networkManager = new QNetworkAccessManager;
networkRequest.setUrl(QUrl(channelUrl));
networkManager->setCookieJar(m_cookJar);
QNetworkReply *reply = networkManager->get(networkRequest);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
reply->deleteLater();
QByteArray buf = reply->readAll();
//qDebug() << buf;
QJsonParseError jsonError;
QJsonDocument jsonRes = QJsonDocument::fromJson(buf, &jsonError);;
if(jsonError.error == QJsonParseError::NoError)
{
if(jsonRes.isObject())
{
QVariantMap result = jsonRes.toVariant().toMap();
foreach(QVariant channel, result["channel_list"].toList())
{
QVariantMap channelMap = channel.toMap();
CHANNEL_INFO channelInfo;
channelInfo.channelId = channelMap["channel_id"].toString();
channelInfo.channelName = channelMap["channel_name"].toString();
channelInfoList.append(channelInfo);
}
}
}
return channelInfoList;
}