dylan revised this gist . Go to revision
1 file changed, 3 insertions, 2 deletions
help.md
| @@ -68,8 +68,8 @@ void MeeTooter::onReplyFinished(QNetworkReply *reply) { | |||
| 68 | 68 | ||
| 69 | 69 | if (start != -1 && end != -1) { | |
| 70 | 70 | QStringList items = response.mid(start + 1, end - start - 1).split("},", QString::SkipEmptyParts); | |
| 71 | - | for (QString item : items) { | |
| 72 | - | item = item.trimmed(); | |
| 71 | + | for (int i = 0; i < items.size(); ++i) { | |
| 72 | + | QString item = items[i].trimmed(); | |
| 73 | 73 | if (item.endsWith("}")) { | |
| 74 | 74 | item = item.left(item.length() - 1); // 移除结尾的 } | |
| 75 | 75 | } | |
| @@ -91,6 +91,7 @@ void MeeTooter::onReplyFinished(QNetworkReply *reply) { | |||
| 91 | 91 | ||
| 92 | 92 | reply->deleteLater(); | |
| 93 | 93 | } | |
| 94 | + | ||
| 94 | 95 | ``` | |
| 95 | 96 | ||
| 96 | 97 | ### 说明 | |
dylan revised this gist . Go to revision
1 file changed, 110 insertions
help.md(file created)
| @@ -0,0 +1,110 @@ | |||
| 1 | + | 如果你决定手动处理 JSON 数据,可以根据 Mastodon API 返回的 JSON 格式,使用字符串解析的方法提取所需的信息。以下是修改过后的 `MeeTooter` 类的代码,使用手动方式处理 JSON 数据。 | |
| 2 | + | ||
| 3 | + | ### 1. 头文件 (`MeeTooter.h`) | |
| 4 | + | ||
| 5 | + | 保持不变: | |
| 6 | + | ||
| 7 | + | ```cpp | |
| 8 | + | #ifndef MEETOOTER_H | |
| 9 | + | #define MEETOOTER_H | |
| 10 | + | ||
| 11 | + | #include <QObject> | |
| 12 | + | #include <QNetworkAccessManager> | |
| 13 | + | #include <QNetworkRequest> | |
| 14 | + | #include <QNetworkReply> | |
| 15 | + | #include <QVariantList> | |
| 16 | + | ||
| 17 | + | class MeeTooter : public QObject { | |
| 18 | + | Q_OBJECT | |
| 19 | + | ||
| 20 | + | public: | |
| 21 | + | explicit MeeTooter(QObject *parent = 0); // 使用 0 代替 nullptr | |
| 22 | + | void fetchPublicTimeline(const QString &url); | |
| 23 | + | ||
| 24 | + | signals: | |
| 25 | + | void timelineFetched(const QVariantList &timeline); | |
| 26 | + | ||
| 27 | + | private slots: | |
| 28 | + | void onReplyFinished(QNetworkReply *reply); | |
| 29 | + | ||
| 30 | + | private: | |
| 31 | + | QNetworkAccessManager *networkManager; | |
| 32 | + | }; | |
| 33 | + | ||
| 34 | + | #endif // MEETOOTER_H | |
| 35 | + | ``` | |
| 36 | + | ||
| 37 | + | ### 2. 实现文件 (`MeeTooter.cpp`) | |
| 38 | + | ||
| 39 | + | 使用手动解析的方法: | |
| 40 | + | ||
| 41 | + | ```cpp | |
| 42 | + | #include "MeeTooter.h" | |
| 43 | + | #include <QDebug> | |
| 44 | + | #include <QStringList> | |
| 45 | + | ||
| 46 | + | MeeTooter::MeeTooter(QObject *parent) : QObject(parent) { | |
| 47 | + | networkManager = new QNetworkAccessManager(this); | |
| 48 | + | connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*))); | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | void MeeTooter::fetchPublicTimeline(const QString &url) { | |
| 52 | + | QNetworkRequest request(QUrl(url + "/api/v1/timelines/public")); // 使用 Mastodon API 获取公共时间线 | |
| 53 | + | networkManager->get(request); | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | void MeeTooter::onReplyFinished(QNetworkReply *reply) { | |
| 57 | + | if (reply->error() == QNetworkReply::NoError) { | |
| 58 | + | QByteArray responseData = reply->readAll(); | |
| 59 | + | ||
| 60 | + | // 手动解析 JSON 数据 | |
| 61 | + | QVariantList timeline; | |
| 62 | + | QString response(responseData); | |
| 63 | + | ||
| 64 | + | // 假设返回的 JSON 格式类似于 [{"content": "...", ...}, {...}, ...] | |
| 65 | + | // 你可以根据实际的 JSON 格式进行字符串查找和切割 | |
| 66 | + | int start = response.indexOf("["); | |
| 67 | + | int end = response.indexOf("]"); | |
| 68 | + | ||
| 69 | + | if (start != -1 && end != -1) { | |
| 70 | + | QStringList items = response.mid(start + 1, end - start - 1).split("},", QString::SkipEmptyParts); | |
| 71 | + | for (QString item : items) { | |
| 72 | + | item = item.trimmed(); | |
| 73 | + | if (item.endsWith("}")) { | |
| 74 | + | item = item.left(item.length() - 1); // 移除结尾的 } | |
| 75 | + | } | |
| 76 | + | int contentIndex = item.indexOf("\"content\":"); | |
| 77 | + | if (contentIndex != -1) { | |
| 78 | + | int startQuote = item.indexOf("\"", contentIndex + 9); | |
| 79 | + | int endQuote = item.indexOf("\"", startQuote + 1); | |
| 80 | + | if (startQuote != -1 && endQuote != -1) { | |
| 81 | + | QString content = item.mid(startQuote + 1, endQuote - startQuote - 1); | |
| 82 | + | timeline.append(content); | |
| 83 | + | } | |
| 84 | + | } | |
| 85 | + | } | |
| 86 | + | emit timelineFetched(timeline); | |
| 87 | + | } | |
| 88 | + | } else { | |
| 89 | + | qDebug() << "Network error occurred:" << reply->errorString(); | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | reply->deleteLater(); | |
| 93 | + | } | |
| 94 | + | ``` | |
| 95 | + | ||
| 96 | + | ### 说明 | |
| 97 | + | ||
| 98 | + | 1. **手动解析 JSON**: 使用字符串查找方法提取 `content` 字段。该代码假设 JSON 的格式是一个对象数组,每个对象中包含一个 `content` 字段。你可以根据实际 API 的返回格式调整解析逻辑。 | |
| 99 | + | ||
| 100 | + | 2. **错误处理**: 保持网络请求的错误处理,以便在网络请求失败时输出错误信息。 | |
| 101 | + | ||
| 102 | + | 3. **使用 QVariantList**: 将提取的 `content` 添加到 `QVariantList` 中,通过信号 `timelineFetched` 传递到 QML 界面。 | |
| 103 | + | ||
| 104 | + | ### 注意事项 | |
| 105 | + | ||
| 106 | + | - **性能和安全性**: 手动解析字符串的方法相对较脆弱,不如使用成熟的 JSON 库(如 QJson)来得安全和高效。如果将来需要处理更复杂的 JSON,建议考虑使用更安全的解析方法。 | |
| 107 | + | ||
| 108 | + | - **API 变化**: 如果 Mastodon 的 API 返回格式发生变化,你需要相应更新解析逻辑。 | |
| 109 | + | ||
| 110 | + | 这样,MeeTooter 应用可以手动处理 JSON 数据,同时保持兼容性。如果有其他问题或需要进一步帮助,请告诉我! | |
Newer
Older