can't get JSON file from QNetworkReply



  • hello, i don't know what is happening to my Qt
    i have to get a JSON file from a request to Google Maps API Geocoding, when i send a request using QNetworkAccessManager, it shown in the consol reply->size() == 0 so there is no file returned
    but when i load it in a web Page with QWebEngineView pageWeb->load(QUrl(url2)); i can se the JSON file

    i don't know why it not work by a request and it work in a web page

    this is the code :

    QUrl url2 ("https://maps.googleapis.com/maps/api/geocode/json?address="+adresse + ",+Algérie&key="+MY_API_KEY);
    QNetworkRequest request;
    request.setUrl(url2);
    m_pNetworkAccessManager->get(request);
    connect(m_pNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    
    pageWeb->load(QUrl(url2));
    pageWeb->show();
    
    void MainWindow::replyFinished(QNetworkReply *reply)
    {
        std::cerr << (reply->size()) << endl;
    
        QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
        QJsonObject rootObj = document.object();
    
        if(rootObj.empty())
            ui->statusBar->showMessage("vide");
        else
            ui->statusBar->showMessage("ayah machi vide !!!");
    }
    


  • resolved !!
    i've chenged

    m_pNetworkAccessManager->get(request);
    

    by

    request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    QByteArray par="";
    m_pNetworkAccessManager->post(request,par);
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.