for test

postReq xx::initAI01()
{
    postReq l_postReq;
    urlVector = std::vector<postReqInfo> {
        {"http://127.0.0.1:6666/Link1", {}},
        {"http://127.0.0.1:6666/Link2", {}},
        {"http://127.0.0.1:6666/Link3", {}}
    };

    // 初始化每个请求结构体中的索引值
    size_t i = 0;
    for (; i < urlVector.size(); ++i) {
        urlVector[i].index = i;
    }
    int l_index = 0;
    bool l_bReply;

    string l_urlToken;

    //for(const auto& url : urlVector) {
    for (auto& requestInfo : urlVector) {
       m_mutex.lock();
       l_index = requestInfo.index;
       switch (requestInfo.index) {
       case 1:
           requestInfo.jsonData["jsonTest"] = "";
           break;
       default:
           break;
       }
       Json::Value jsonObject;
       SendPostRequest(requestInfo.url, [&](QNetworkReply *reply) {
           std::cout << (requestInfo.index + 1) << " init SendPostRequest" << std::endl;
           m_strReply = ReplyFinished(reply);
           l_bReply =!m_strReply.empty();
           if (l_bReply) {
               std::cout << "success get data" << std::endl;
               if(requestInfo.index != i - 1) {
                   Json::Value jsonObject;
                   Json::Reader jsonReader;
                   if(m_mainServer.parseJson(m_strReply, jsonObject)) {
                       l_postReq.retValue = -2;
                   } else {
                       if(false == jsonObject["code"].isInt()) {
                           l_postReq.retValue = -3;
                           return;
                       }
                       if(0 != jsonObject["code"].asInt()) {
                           l_postReq.retValue = -4;
                       }
                   }
                   l_postReq.jsonData = jsonObject;
               } //最后一个url则退出
           } else {
               std::cout << "failed to get data" << std::endl;
               l_postReq.retValue = -1;
           }
           m_mutex.unlock();
           emit requestFinished(requestInfo.index, l_bReply);
       }, requestInfo.jsonData);

       QEventLoop eventLoop;
       connect(this, &xx::requestFinished, &eventLoop, [&eventLoop, &requestInfo, &l_urlToken](int index, bool l_bReply) {
           l_urlToken = requestInfo.url.substr();

           size_t pos = requestInfo.url.find_last_of("/\\");
           if (pos != std::string::npos)
               l_urlToken = requestInfo.url.substr(pos+1);
           else
               l_urlToken = requestInfo.url;
           if(index == requestInfo.index) {
               eventLoop.quit();
               if(false == l_bReply)
                   return;
           }
       });
       eventLoop.exec();

       if(0 != l_postReq.retValue)
           break;

    }
}
void xx::SendPostRequest(const std::string &url, const std::function<void(QNetworkReply*)> &callback, const Json::Value &jsonObject) {
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QNetworkRequest request;
    request.setUrl(QUrl(QString::fromStdString(url)));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-from-urlencoded;charset=UTF-8");

    cout<<"into SendPostRequest func"<<endl;
    // 使用Json::StreamWriterBuilder将jsonObject序列化为字符串
    Json::StreamWriterBuilder writer;
    //std::string outputString = Json::writeString(writer, jsonObject);
    // 如果你想单独打印数组部分,可以提取并序列化数组
    Json::Value arrayPart;

    try {
        writer["indentation"] = "";  // 设置缩进为空,这样就不会有换行和缩进了
        std::unique_ptr<Json::StreamWriter> noIndentation(writer.newStreamWriter());
        std::string outputString = Json::writeString(writer, jsonObject);
        printf("outputString : %s\n", outputString.c_str());
        // 后续逻辑基于outputString进行操作
    } catch (const Json::Exception& err) {
        std::cerr << "JSON序列化出错: " << err.what() << std::endl;
    }

    const QByteArray postData = QString::fromStdString(Json::writeString(writer, jsonObject)).toUtf8(); // 填充你的JSON数据
qDebug()<<postData;
    QNetworkReply *reply = manager->post(request, postData);

    connect(reply, &QNetworkReply::finished, this, [=]() {
        qDebug()<<"readALL:  "<<reply->readAll();
        if(callback)
            callback(reply);
        reply->deleteLater();
    });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值