Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved can't get JSON file from QNetworkReply

    General and Desktop
    qnetreply qnetworkrequest qwebengineview qnetworkaccessm json
    1
    2
    4862
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nazimGT
      nazimGT last edited by

      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 !!!");
      }
      
      1 Reply Last reply Reply Quote 0
      • nazimGT
        nazimGT last edited by

        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);
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post