-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendrequest.cpp
49 lines (39 loc) · 1.25 KB
/
sendrequest.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
#include "sendrequest.h"
SendRequest::SendRequest(QObject *parent)
: QObject{parent}
{
}
void SendRequest::tryRequest(QString giveUrl)
{
QString url = giveUrl;
//qDebug() << "giveUrl = " << giveUrl;
reply = manager.get(QNetworkRequest(QUrl(url)));
QObject::connect(reply, &QNetworkReply::finished, this, &SendRequest::finishRequest);
}
void SendRequest::finishRequest()
{
if (reply ->error() == QNetworkReply::NoError)
{
qDebug() << "No error. Internet connection ok";
QByteArray data = reply->readAll();
QJsonDocument jsondocument = QJsonDocument::fromJson(data);
obj = jsondocument.object();
emit finishJsonObjectCreate(obj);
}
else
{
QFile file;
file.setFileName(":/resources/test.json");
if (file.open(QIODevice::ReadOnly))
{
while (!file.atEnd())
{
qDebug() << "Нет интернет соединения. Файл " << file.fileName() << " открыт!";
QByteArray data = file.readAll();
QJsonDocument jsondocument = QJsonDocument::fromJson(data);
obj = jsondocument.object();
emit finishJsonObjectCreate(obj);
}
}
}
}