-
Notifications
You must be signed in to change notification settings - Fork 22
/
getsongplaylistid.cpp
57 lines (41 loc) · 1.43 KB
/
getsongplaylistid.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 "getsongplaylistid.h"
const QString playListUrl = "http://fm.baidu.com/dev/api/?tn=playlist&format=json&id=";
GetSongPlayListId::GetSongPlayListId(QObject *parent, QNetworkCookieJar *cookie) : QObject(parent)
{
m_cookJar = cookie;
}
QList<QString> GetSongPlayListId::getSongIdList(QString channelId)
{
QNetworkRequest networkRequest;
QNetworkAccessManager *networkManager = new QNetworkAccessManager;
networkRequest.setUrl(QUrl(playListUrl + channelId));
networkManager->setCookieJar(m_cookJar);
QNetworkReply *reply = networkManager->get(networkRequest);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
reply->deleteLater();
QList<QString> songId;
QByteArray buf = reply->readAll();
QJsonParseError jsonError;
QJsonDocument jsonRes = QJsonDocument::fromJson(buf, &jsonError);;
if(jsonError.error == QJsonParseError::NoError)
{
if(jsonRes.isObject())
{
QVariantMap result = jsonRes.toVariant().toMap();
foreach(QVariant song, result["list"].toList())
{
QVariantMap songMap = song.toMap();
QString id = songMap["id"].toString();
//如果歌曲ID有效
if(!id.isEmpty())
{
songId.append(id);
}
}
}
}
// qDebug() << buf;
return songId;
}