fix network on pointed thread
This commit is contained in:
parent
8c2f581fc8
commit
d1a00a716c
@ -130,6 +130,7 @@ HEADERS += \
|
||||
utils/DoricImageBridge.h \
|
||||
utils/DoricLayouts.h \
|
||||
utils/DoricMouseAreaBridge.h \
|
||||
utils/DoricNetworkService.h \
|
||||
utils/DoricObjectFactory.h \
|
||||
utils/DoricThreadMode.h \
|
||||
utils/DoricUtils.h \
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "DoricNetworkPlugin.h"
|
||||
#include "utils/DoricNetworkService.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
@ -7,35 +9,10 @@ void DoricNetworkPlugin::request(QString jsValueString, QString callbackId) {
|
||||
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
|
||||
QJsonValue jsValue = document.object();
|
||||
|
||||
QString url = jsValue["url"].toString();
|
||||
QString method = jsValue["method"].toString();
|
||||
|
||||
QJsonValue headerVal = jsValue["headers"];
|
||||
QJsonValue dataVal = jsValue["data"];
|
||||
QJsonValue timeoutVal = jsValue["timeout"];
|
||||
|
||||
if (headerVal.isObject()) {
|
||||
foreach (QString key, headerVal.toObject().keys()) {
|
||||
httpRequest.setRawHeader(key.toUtf8(),
|
||||
headerVal[key].toString().toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
QObject::connect(&networkAccessManager, SIGNAL(finished(QNetworkReply *)),
|
||||
this, SLOT(networkRequestFinished(QNetworkReply *)));
|
||||
|
||||
if (method == "get") {
|
||||
httpRequest.setUrl(QUrl(url));
|
||||
networkAccessManager.get(httpRequest);
|
||||
}
|
||||
}
|
||||
void DoricNetworkPlugin::networkRequestFinished(QNetworkReply *reply) {
|
||||
int statusCode =
|
||||
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
} else {
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
DoricNetworkService::getInstance()->request(jsValue, qApp,
|
||||
[](int code, QByteArray data) {
|
||||
qDebug() << code;
|
||||
qDebug() << data;
|
||||
qDebug() << "";
|
||||
});
|
||||
}
|
||||
|
@ -3,23 +3,11 @@
|
||||
|
||||
#include "DoricNativePlugin.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
class DoricNetworkPlugin : public DoricNativePlugin {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using DoricNativePlugin::DoricNativePlugin;
|
||||
|
||||
Q_INVOKABLE void request(QString jsValueString, QString callbackId);
|
||||
|
||||
private:
|
||||
QNetworkRequest httpRequest;
|
||||
QNetworkAccessManager networkAccessManager;
|
||||
|
||||
public slots:
|
||||
void networkRequestFinished(QNetworkReply *reply);
|
||||
};
|
||||
#endif // DORICNETWORKPLUGIN_H
|
||||
|
93
doric-Qt/doric/utils/DoricNetworkService.h
Normal file
93
doric-Qt/doric/utils/DoricNetworkService.h
Normal file
@ -0,0 +1,93 @@
|
||||
#ifndef DORICNETWORKSERVICE_H
|
||||
#define DORICNETWORKSERVICE_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QThread>
|
||||
|
||||
class InnerTask : public QObject {
|
||||
Q_OBJECT
|
||||
private:
|
||||
QNetworkRequest httpRequest;
|
||||
QNetworkAccessManager networkAccessManager;
|
||||
|
||||
public:
|
||||
explicit InnerTask() {
|
||||
connect(this, &InnerTask::run, this, &InnerTask::innerRun);
|
||||
}
|
||||
|
||||
void innerRun(QJsonValue jsValue) {
|
||||
QString url = jsValue["url"].toString();
|
||||
QString method = jsValue["method"].toString();
|
||||
|
||||
QJsonValue headerVal = jsValue["headers"];
|
||||
QJsonValue dataVal = jsValue["data"];
|
||||
QJsonValue timeoutVal = jsValue["timeout"];
|
||||
|
||||
if (headerVal.isObject()) {
|
||||
foreach (QString key, headerVal.toObject().keys()) {
|
||||
httpRequest.setRawHeader(key.toUtf8(),
|
||||
headerVal[key].toString().toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
if (method == "get") {
|
||||
httpRequest.setUrl(QUrl(url));
|
||||
QNetworkReply *reply = networkAccessManager.get(httpRequest);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply] {
|
||||
int statusCode =
|
||||
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
emit response(statusCode, reply->readAll());
|
||||
} else {
|
||||
emit response(statusCode, QByteArray());
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
this->deleteLater();
|
||||
});
|
||||
}
|
||||
}
|
||||
signals:
|
||||
void run(QJsonValue jsValue);
|
||||
void response(int code, QByteArray data);
|
||||
};
|
||||
|
||||
class DoricNetworkService : public QObject {
|
||||
Q_OBJECT
|
||||
private:
|
||||
QThread thread;
|
||||
|
||||
static DoricNetworkService *local_instance;
|
||||
DoricNetworkService() {
|
||||
qDebug() << "constructor";
|
||||
thread.start();
|
||||
}
|
||||
|
||||
~DoricNetworkService() { qDebug() << "destructor"; }
|
||||
|
||||
public:
|
||||
static DoricNetworkService *getInstance() {
|
||||
static DoricNetworkService instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
QThread *getThread() { return &thread; }
|
||||
|
||||
template <typename T>
|
||||
void request(QJsonValue jsValue, QObject *receiver, T lambda) {
|
||||
InnerTask *innerTask = new InnerTask();
|
||||
innerTask->moveToThread(&thread);
|
||||
|
||||
connect(innerTask, &InnerTask::response, receiver, lambda);
|
||||
innerTask->run(jsValue);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // DORICNETWORKSERVICE_H
|
Reference in New Issue
Block a user