0

这是我的代码。它从数据库中检索想要的信息,但它没有进入 foreach 循环,所以我不能将响应数据包含到 saldo 属性中。

void engine::makeSaldoRequest(){

    QString site_url="http://localhost:3000/tili/1";
      QString credentials="automaatti1:raha123";
      QNetworkRequest request((site_url));
      request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      QByteArray data = credentials.toLocal8Bit().toBase64();
      QString headerData = "Basic " + data;
      request.setRawHeader( "Authorization", headerData.toLocal8Bit());

      qDebug() << "haetaan saldo";

      manager = new QNetworkAccessManager(this);
      connect(manager, SIGNAL(finished (QNetworkReply*)),
                    this, SLOT(saldoReplySlot(QNetworkReply*)));

      saldoReply = manager->get(request);

}


void engine::saldoReplySlot(QNetworkReply *saldoreply){

    QByteArray response_data=saldoreply->readAll();
    qDebug() << response_data;
    QJsonDocument json_doc=QJsonDocument::fromJson(response_data);
    QJsonArray json_array=json_doc.array();
    QString saldo;

    foreach(const QJsonValue &value, json_array){
        QJsonObject json_object=value.toObject();

        saldo+=QString::number((json_object["idtili"].toInt()))+" "+
                json_object["tilin_saldo"].toString()+"\r\n";
        qDebug() << "loopissa";
    }

    qDebug() << "lähetetään signaali exelle missä saldo on";
    emit saldoToExe(saldo);
    qDebug() << saldo;

    manager->deleteLater();
    saldoReply->deleteLater();
    saldoreply->deleteLater();

}
4

0 回答 0