This commit is contained in:
Gangphon 2023-07-24 11:25:14 +08:00
parent 2cfa0cc514
commit 9b767de1d0
4 changed files with 23 additions and 22 deletions

View File

@ -274,7 +274,7 @@ UpgradeApkDialog::UpgradeApkDialog(QWidget *parent) : QDialog(parent) {
json.insert("packageName", strApkName); json.insert("packageName", strApkName);
auto reply = NetReq("http://"+item->mCard.ip+":2016/settings").timeout(120000).post(json); auto reply = NetReq("http://"+item->mCard.ip+":2016/settings").timeout(120000).post(json);
connect(reply, &QNetworkReply::finished, this, [reply, item, strApkName] { connect(reply, &QNetworkReply::finished, this, [reply, item, strApkName] {
QString err = errStrWithData(reply); QString err = checkReplyForJson(reply, "error");
if(! err.isEmpty()) { if(! err.isEmpty()) {
item->setResult(tr("Uninstall error")+": "+err, Qt::red); item->setResult(tr("Uninstall error")+": "+err, Qt::red);
return; return;

View File

@ -1,6 +1,7 @@
#include "globaldefine.h" #include "globaldefine.h"
#include "devicepanel.h" #include "devicepanel.h"
#include "deviceitem.h" #include "deviceitem.h"
#include "gutil/qnetwork.h"
#include <QDir> #include <QDir>
#include <QDateTime> #include <QDateTime>
#include <QCoreApplication> #include <QCoreApplication>
@ -24,25 +25,8 @@ DeviceItem *findItem(QString id) {
return 0; 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<QNetworkReply::NetworkError>().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) { QString checkReply(QNetworkReply *reply, QJsonDocument *outJson) {
auto err = replyErr(reply); auto err = errStr(reply);
if(! err.isEmpty()) { if(! err.isEmpty()) {
auto data = reply->readAll(); auto data = reply->readAll();
if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data; if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
@ -57,7 +41,7 @@ QString checkReply(QNetworkReply *reply, QJsonDocument *outJson) {
return ""; return "";
} }
QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArray *outData) { QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArray *outData) {
auto err = replyErr(reply); auto err = errStr(reply);
auto data = reply->readAll(); auto data = reply->readAll();
if(outData) *outData = data; if(outData) *outData = data;
if(! err.isEmpty()) { if(! err.isEmpty()) {
@ -71,6 +55,22 @@ QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArr
if(outJson) outJson->swap(json); if(outJson) outJson->swap(json);
return ""; 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) { quint64 dirFileSize(const QString &path) {
QDir dir(path); QDir dir(path);

View File

@ -83,9 +83,9 @@ inline int verCompare(const QString& a, const QString& b) {
return aparts.count() > bparts.count() ? aparts[cnt].toInt() : bparts[cnt].toInt(); return aparts.count() > bparts.count() ? aparts[cnt].toInt() : bparts[cnt].toInt();
} }
QString replyErr(QNetworkReply *);
QString checkReply(QNetworkReply *, QJsonDocument * = 0); QString checkReply(QNetworkReply *, QJsonDocument * = 0);
QString checkReplyForJson(QNetworkReply *, QJsonDocument * = 0, QByteArray * = 0); QString checkReplyForJson(QNetworkReply *, QJsonDocument * = 0, QByteArray * = 0);
QString checkReplyForJson(QNetworkReply *, QString errField);
#define Def_CtrlReqPre \ #define Def_CtrlReqPre \
waitingDlg->show();\ waitingDlg->show();\

View File

@ -18,8 +18,9 @@ public:
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {}; explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {};
#else #else
NetReq() {init();}; 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(const QUrl &url) : QNetworkRequest{url} {init();};
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {init();};
inline void init() { inline void init() {
setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
} }