diff --git a/LedOK/device/upgradeapkdialog.cpp b/LedOK/device/upgradeapkdialog.cpp index 9a2a658..fa0e163 100644 --- a/LedOK/device/upgradeapkdialog.cpp +++ b/LedOK/device/upgradeapkdialog.cpp @@ -274,7 +274,7 @@ UpgradeApkDialog::UpgradeApkDialog(QWidget *parent) : QDialog(parent) { json.insert("packageName", strApkName); auto reply = NetReq("http://"+item->mCard.ip+":2016/settings").timeout(120000).post(json); connect(reply, &QNetworkReply::finished, this, [reply, item, strApkName] { - QString err = errStrWithData(reply); + QString err = checkReplyForJson(reply, "error"); if(! err.isEmpty()) { item->setResult(tr("Uninstall error")+": "+err, Qt::red); return; diff --git a/LedOK/globaldefine.cpp b/LedOK/globaldefine.cpp index 3071763..b494352 100644 --- a/LedOK/globaldefine.cpp +++ b/LedOK/globaldefine.cpp @@ -1,6 +1,7 @@ #include "globaldefine.h" #include "devicepanel.h" #include "deviceitem.h" +#include "gutil/qnetwork.h" #include #include #include @@ -24,25 +25,8 @@ DeviceItem *findItem(QString id) { return 0; } -QString replyErr(QNetworkReply *reply) { - reply->deleteLater(); - auto error = reply->error(); - if(error != QNetworkReply::NoError) { - auto errStr = reply->errorString(); - if(error!=QNetworkReply::InternalServerError || ! errStr.endsWith("replied: Unknown")) { - if(error==QNetworkReply::OperationCanceledError) { - error = QNetworkReply::TimeoutError; - errStr = QCoreApplication::translate("Def","Connection Timeout"); - } - return QString(QMetaEnum::fromType().valueToKey(error))+" ("+QString::number(error)+") "+errStr; - } - } - auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if(status != 200) return QString::number(status)+" "+reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); - return ""; -} QString checkReply(QNetworkReply *reply, QJsonDocument *outJson) { - auto err = replyErr(reply); + auto err = errStr(reply); if(! err.isEmpty()) { auto data = reply->readAll(); if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data; @@ -57,7 +41,7 @@ QString checkReply(QNetworkReply *reply, QJsonDocument *outJson) { return ""; } QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArray *outData) { - auto err = replyErr(reply); + auto err = errStr(reply); auto data = reply->readAll(); if(outData) *outData = data; if(! err.isEmpty()) { @@ -71,6 +55,22 @@ QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArr if(outJson) outJson->swap(json); return ""; } +QString checkReplyForJson(QNetworkReply *reply, QString errField) { + auto err = errStr(reply); + auto data = reply->readAll(); + if(! err.isEmpty()) { + if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data; + return err; + } + QJsonParseError jsonErr; + QJsonDocument json = QJsonDocument::fromJson(data, &jsonErr); + if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data; + if(! json["success"].toBool()) { + auto errStr = json[errField].toString(); + return QCoreApplication::translate("Def","Fail")+". "+QCoreApplication::translate("Def","Device replied")+": "+(errStr.isEmpty() ? data : errStr); + } + return ""; +} quint64 dirFileSize(const QString &path) { QDir dir(path); diff --git a/LedOK/globaldefine.h b/LedOK/globaldefine.h index 7cec49f..1ead39e 100644 --- a/LedOK/globaldefine.h +++ b/LedOK/globaldefine.h @@ -83,9 +83,9 @@ inline int verCompare(const QString& a, const QString& b) { return aparts.count() > bparts.count() ? aparts[cnt].toInt() : bparts[cnt].toInt(); } -QString replyErr(QNetworkReply *); QString checkReply(QNetworkReply *, QJsonDocument * = 0); QString checkReplyForJson(QNetworkReply *, QJsonDocument * = 0, QByteArray * = 0); +QString checkReplyForJson(QNetworkReply *, QString errField); #define Def_CtrlReqPre \ waitingDlg->show();\ diff --git a/LedOK/gutil/qnetwork.h b/LedOK/gutil/qnetwork.h index 70386a7..a2e15cb 100644 --- a/LedOK/gutil/qnetwork.h +++ b/LedOK/gutil/qnetwork.h @@ -18,8 +18,9 @@ public: explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {}; #else NetReq() {init();}; - explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {init();}; + explicit NetReq(const QString &url) : QNetworkRequest{url} {init();}; explicit NetReq(const QUrl &url) : QNetworkRequest{url} {init();}; + explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {init();}; inline void init() { setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); }