当前位置:   article > 正文

通过网络api获取日期对应的节假日信息

通过网络api获取日期对应的节假日信息

网络接口获取链接:免费节假日API_原百度节假日API

HolidayJudge.h

  1. #pragma once
  2. #include <QtWidgets/QWidget>
  3. #include "ui_HolidayJudge.h"
  4. enum DATESTATE
  5. {
  6. WORK=0,//工作日
  7. DAYOFF,//休息日
  8. HOLIDAY//节假日
  9. };
  10. class HolidayJudge : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. HolidayJudge(QWidget *parent = nullptr);
  15. ~HolidayJudge();
  16. private slots:
  17. void slotQueryGet();
  18. void slotQueryPost();
  19. private:
  20. void init();
  21. private:
  22. Ui::HolidayJudgeClass ui;
  23. QMap<DATESTATE, QString> m_dateInfoMap;
  24. };

HolidayJudge.cpp

  1. #include "HolidayJudge.h"
  2. #include <QNetworkAccessManager>
  3. #include <QNetworkReply>
  4. #include <QList>
  5. #include <QSslError>
  6. HolidayJudge::HolidayJudge(QWidget *parent)
  7. : QWidget(parent)
  8. {
  9. ui.setupUi(this);
  10. init();
  11. QObject::connect(ui.pushButton_queryGet, &QPushButton::clicked, this, &HolidayJudge::slotQueryGet);
  12. QObject::connect(ui.pushButton_queryPost, &QPushButton::clicked, this, &HolidayJudge::slotQueryPost);
  13. }
  14. HolidayJudge::~HolidayJudge()
  15. {}
  16. void HolidayJudge::slotQueryGet()
  17. {
  18. QNetworkAccessManager* manager = new QNetworkAccessManager(this);
  19. connect(manager, &QNetworkAccessManager::finished,
  20. this, [=](QNetworkReply* reply) {
  21. //ui.textBrowser->append(QString(reply->readAll()));
  22. int val = QString(reply->readAll()).toInt();//0:工作日,1:休息日,2:节假日
  23. QString info = ui.dateEdit->date().toString("yyyy/MM/dd") + QString("是%1").arg(m_dateInfoMap.value(static_cast<DATESTATE>(val)));
  24. ui.textBrowser->append("get: " + info);
  25. reply->deleteLater();
  26. });
  27. QString date = ui.dateEdit->date().toString("yyyyMMdd");
  28. QUrl url("http://tool.bitefu.net/jiari/?d=" + date);
  29. manager->get(QNetworkRequest(url));
  30. //开启事件循环,等待直到服务器返回数据后再执行
  31. //QEventLoop eventLoop;
  32. //connect(manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
  33. //eventLoop.exec();
  34. manager->deleteLater();
  35. }
  36. void HolidayJudge::slotQueryPost()
  37. {
  38. QNetworkAccessManager* accessManager = new QNetworkAccessManager(this);
  39. QNetworkRequest request;
  40. QString date = ui.dateEdit->date().toString("yyyyMMdd");
  41. QUrl url("http://tool.bitefu.net/jiari/?d=" + date);
  42. request.setUrl(url);
  43. request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
  44. QNetworkReply* reply = accessManager->post(request, QByteArray());
  45. QObject::connect(reply, &QNetworkReply::readyRead, this, [this, reply]() {
  46. int val = QString(reply->readAll()).toInt();//0:工作日,1:休息日,2:节假日
  47. QString info = ui.dateEdit->date().toString("yyyy/MM/dd") + QString("是%1").arg(m_dateInfoMap.value(static_cast<DATESTATE>(val)));
  48. ui.textBrowser->append("post: " + info);
  49. reply->deleteLater();
  50. });
  51. QObject::connect(reply, &QNetworkReply::errorOccurred,
  52. this, [=](QNetworkReply::NetworkError error) {
  53. });
  54. QObject::connect(reply, &QNetworkReply::sslErrors,
  55. this, [=](const QList<QSslError>& error){
  56. });
  57. }
  58. void HolidayJudge::init()
  59. {
  60. m_dateInfoMap.insert(WORK, "工作日");
  61. m_dateInfoMap.insert(DAYOFF, "休息日");
  62. m_dateInfoMap.insert(HOLIDAY, "节假日");
  63. }

效果展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/392474
推荐阅读
相关标签
  

闽ICP备14008679号