QT HTTP请求(get和post)+json解析

1、get
QT HTTP请求(get和post)+json解析_第1张图片

  QNetworkAccessManager *smsManager = new QNetworkAccessManager(this);
  QString url = "http://123.56.81.6:8080/api/smsCode";
  QNetworkRequest *Request = new QNetworkRequest(QUrl(url));
  Request ->setRawHeader(QByteArray("SESSION-ID"),_p->m_SessionID);
  QByteArray responseData;
  QEventLoop eventLoop;
  connect(smsManager , SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
  QNetworkReply* initReply = smsManager ->get(*Request );
  eventLoop.exec();       //block until finish
  responseData = initReply->readAll();

  //解析json
  QString imageData;//接收到服务器的base64数据是string类型
  QString txt;
  QJsonParseError json_error;
  QJsonDocument doucment = QJsonDocument::fromJson(responseData, &json_error);
  if (json_error.error == QJsonParseError::NoError) {
    if (doucment.isObject()) {
      const QJsonObject obj = doucment.object();
      qDebug() << obj;
      if (obj.contains("data")) {
        QJsonObject object_data = obj.value("data").toObject();
        if (object_data.contains("base64Image"))
        {
          imageData = object_data.value("base64Image").toString();
        }
        if (object_data.contains("text"))
        {
          txt = object_data.value("text").toString();
        }
      }
    }
  }

2、post
QT HTTP请求(get和post)+json解析_第2张图片

  QNetworkAccessManager *loginManager = new QNetworkAccessManager(this);
  QNetworkRequest *Request = new QNetworkRequest(QUrl("http://123.56.81.6:8080/api/user/login"));
  Request->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
  Request->setRawHeader(QByteArray("SESSION-ID"), _p->m_SessionID);
  QByteArray responseData;
  QEventLoop eventLoop;
  connect(loginManager , SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));

  QString user = ui.user->text();
  QString pass = ui.passwd->text();
  QString sms = ui.smscode->text();

  QJsonObject obj;
  obj.insert("password", pass);
  obj.insert("smsCode", sms);
  obj.insert("username", user);
  QByteArray byteArray = QJsonDocument(obj).toJson(QJsonDocument::Compact);
  QString str(byteArray);

  QNetworkReply* initReply = loginManager->post(*Request, str.toUtf8());
  eventLoop.exec();       //block until finish
  responseData = initReply->readAll();

  //解析json
  QJsonParseError json_error;
  QJsonDocument doucment = QJsonDocument::fromJson(responseData, &json_error);
  if (json_error.error == QJsonParseError::NoError) {
    if (doucment.isObject()) {
      const QJsonObject obj = doucment.object();
      qDebug() << obj;
      if (obj.contains("data")) {
        const QJsonObject obj_data = obj.value("data").toObject();
        if (obj_data.contains("token"))
        {
          QString token = obj_data.value("token").toString();
        }
        if (obj_data.contains("userId"))
        {
          int userID = obj_data.value("userId").toInt();
        }
      }
      if (obj.contains("message")) {
         QString loginMessage = obj.value("message").toString();
      }
      if (obj.contains("success")) {
        bool isSuccess = obj.value("success").toBool();
      }
    }
  }

3、总结
1、在请求时使用QEventLoop操作更加方便。
2、请求头使用setRawHeader函数
3、QJsonDocument(obj).toJson(QJsonDocument::Compact);这里一定要注意json格式
QT HTTP请求(get和post)+json解析_第3张图片

你可能感兴趣的:(QT,http,json,qt)