-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrobot.cpp
53 lines (49 loc) · 1.44 KB
/
robot.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
#include "robot.h"
#include<QJsonDocument>
#include<QNetworkReply>
Robot::Robot(QObject *parent) : QObject(parent)
{
manager = new QNetworkAccessManager(this);
baseUrl="http://www.tuling123.com/openapi/api";
url.setUrl(baseUrl);
json.insert("key","2886d0c488574d989920eb6767366287");
json.insert("userid","152423");
request=new QNetworkRequest();
request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request->setUrl(url);
}
void Robot::post(QString info)
{
json["info"]=info;
manager->post(*request,QJsonDocument(json).toJson());
}
QString Robot::getResponse() const
{
return response;
}
QNetworkAccessManager *Robot::getManager() const
{
return manager;
}
void Robot::replyFinished(QNetworkReply *reply)
{
// 获取响应信息
QByteArray bytes = reply->readAll();
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
qDebug() << QStringLiteral("fail to read json");
return;
}
// 解析Json
if (doucment.isObject()) {
QJsonObject obj = doucment.object();
QJsonValue value;
if (obj.contains("text")) {
value = obj.take("text");
if (value.isString()) {
this->response= value.toString();
}
}
}
}