网络接口获取链接:免费节假日API_原百度节假日API
HolidayJudge.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_HolidayJudge.h"
enum DATESTATE
{
WORK=0,//工作日
DAYOFF,//休息日
HOLIDAY//节假日
};
class HolidayJudge : public QWidget
{
Q_OBJECT
public:
HolidayJudge(QWidget *parent = nullptr);
~HolidayJudge();
private slots:
void slotQueryGet();
void slotQueryPost();
private:
void init();
private:
Ui::HolidayJudgeClass ui;
QMap<DATESTATE, QString> m_dateInfoMap;
};
HolidayJudge.cpp
#include "HolidayJudge.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QList>
#include <QSslError>
HolidayJudge::HolidayJudge(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
init();
QObject::connect(ui.pushButton_queryGet, &QPushButton::clicked, this, &HolidayJudge::slotQueryGet);
QObject::connect(ui.pushButton_queryPost, &QPushButton::clicked, this, &HolidayJudge::slotQueryPost);
}
HolidayJudge::~HolidayJudge()
{}
void HolidayJudge::slotQueryGet()
{
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished,
this, [=](QNetworkReply* reply) {
//ui.textBrowser->append(QString(reply->readAll()));
int val = QString(reply->readAll()).toInt();//0:工作日,1:休息日,2:节假日
QString info = ui.dateEdit->date().toString("yyyy/MM/dd") + QString("是%1").arg(m_dateInfoMap.value(static_cast<DATESTATE>(val)));
ui.textBrowser->append("get: " + info);
reply->deleteLater();
});
QString date = ui.dateEdit->date().toString("yyyyMMdd");
QUrl url("http://tool.bitefu.net/jiari/?d=" + date);
manager->get(QNetworkRequest(url));
//开启事件循环,等待直到服务器返回数据后再执行
//QEventLoop eventLoop;
//connect(manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
//eventLoop.exec();
manager->deleteLater();
}
void HolidayJudge::slotQueryPost()
{
QNetworkAccessManager* accessManager = new QNetworkAccessManager(this);
QNetworkRequest request;
QString date = ui.dateEdit->date().toString("yyyyMMdd");
QUrl url("http://tool.bitefu.net/jiari/?d=" + date);
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
QNetworkReply* reply = accessManager->post(request, QByteArray());
QObject::connect(reply, &QNetworkReply::readyRead, this, [this, reply]() {
int val = QString(reply->readAll()).toInt();//0:工作日,1:休息日,2:节假日
QString info = ui.dateEdit->date().toString("yyyy/MM/dd") + QString("是%1").arg(m_dateInfoMap.value(static_cast<DATESTATE>(val)));
ui.textBrowser->append("post: " + info);
reply->deleteLater();
});
QObject::connect(reply, &QNetworkReply::errorOccurred,
this, [=](QNetworkReply::NetworkError error) {
});
QObject::connect(reply, &QNetworkReply::sslErrors,
this, [=](const QList<QSslError>& error){
});
}
void HolidayJudge::init()
{
m_dateInfoMap.insert(WORK, "工作日");
m_dateInfoMap.insert(DAYOFF, "休息日");
m_dateInfoMap.insert(HOLIDAY, "节假日");
}
效果展示