Last active 1729664489

dylan revised this gist 1729664489. Go to revision

1 file changed, 45 insertions

gistfile1.txt(file created)

@@ -0,0 +1,45 @@
1 + #include "MeeTooter.h"
2 + #include <QDebug>
3 + #include <qjson/serializer.h> // QJson 序列化器
4 + #include <qjson/parser.h> // QJson 解析器
5 +
6 + MeeTooter::MeeTooter(QObject *parent) : QObject(parent) {
7 + networkManager = new QNetworkAccessManager(this);
8 + connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*)));
9 + }
10 +
11 + void MeeTooter::fetchPublicTimeline(const QString &url) {
12 + QNetworkRequest request(QUrl(url + "/api/v1/timelines/public")); // 使用 Mastodon API 获取公共时间线
13 + networkManager->get(request);
14 + }
15 +
16 + void MeeTooter::onReplyFinished(QNetworkReply *reply) {
17 + if (reply->error() == QNetworkReply::NoError) {
18 + QByteArray responseData = reply->readAll();
19 +
20 + // 使用 QJson 库解析 JSON 数据
21 + QJson::Parser parser;
22 + bool ok;
23 +
24 + QVariant result = parser.parse(responseData, &ok);
25 +
26 + if (ok && result.type() == QVariant::List) {
27 + QVariantList timeline = result.toList();
28 +
29 + QVariantList parsedTimeline;
30 +
31 + foreach (const QVariant &item, timeline) {
32 + QVariantMap itemMap = item.toMap();
33 + parsedTimeline.append(itemMap["content"]); // 假设 content 是你想显示的字段
34 + }
35 +
36 + emit timelineFetched(parsedTimeline);
37 + } else {
38 + qDebug() << "Error parsing JSON data";
39 + }
40 + } else {
41 + qDebug() << "Network error occurred:" << reply->errorString();
42 + }
43 +
44 + reply->deleteLater();
45 + }
Newer Older

Powered by Opengist Load: 13ms