add network plugin
This commit is contained in:
parent
a910f61c2a
commit
189b13927a
@ -1,6 +1,7 @@
|
||||
#include "DoricRegistry.h"
|
||||
|
||||
#include "plugin/DoricModalPlugin.h"
|
||||
#include "plugin/DoricNetworkPlugin.h"
|
||||
#include "plugin/DoricPopoverPlugin.h"
|
||||
#include "plugin/DoricShaderPlugin.h"
|
||||
|
||||
@ -16,6 +17,7 @@ DoricRegistry::DoricRegistry() {
|
||||
registerNativePlugin<DoricShaderPlugin>("shader");
|
||||
registerNativePlugin<DoricModalPlugin>("modal");
|
||||
registerNativePlugin<DoricPopoverPlugin>("popover");
|
||||
registerNativePlugin<DoricNetworkPlugin>("network");
|
||||
|
||||
registerViewNode<DoricRootNode>("Root");
|
||||
registerViewNode<DoricStackNode>("Stack");
|
||||
|
@ -35,6 +35,7 @@ SOURCES += \
|
||||
engine/v8/V8Executor.cpp \
|
||||
main.cpp \
|
||||
plugin/DoricModalPlugin.cpp \
|
||||
plugin/DoricNetworkPlugin.cpp \
|
||||
plugin/DoricPopoverPlugin.cpp \
|
||||
plugin/DoricShaderPlugin.cpp \
|
||||
shader/DoricGroupNode.cpp \
|
||||
@ -108,6 +109,7 @@ HEADERS += \
|
||||
engine/v8/V8Executor.h \
|
||||
plugin/DoricModalPlugin.h \
|
||||
plugin/DoricNativePlugin.h \
|
||||
plugin/DoricNetworkPlugin.h \
|
||||
plugin/DoricPopoverPlugin.h \
|
||||
plugin/DoricShaderPlugin.h \
|
||||
shader/DoricGroupNode.h \
|
||||
|
41
doric-Qt/doric/plugin/DoricNetworkPlugin.cpp
Normal file
41
doric-Qt/doric/plugin/DoricNetworkPlugin.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "DoricNetworkPlugin.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
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();
|
||||
}
|
25
doric-Qt/doric/plugin/DoricNetworkPlugin.h
Normal file
25
doric-Qt/doric/plugin/DoricNetworkPlugin.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef DORICNETWORKPLUGIN_H
|
||||
#define DORICNETWORKPLUGIN_H
|
||||
|
||||
#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
|
Reference in New Issue
Block a user