Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. simple network request return empty string
Forum Updated to NodeBB v4.3 + New Features

simple network request return empty string

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 667 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    sodu
    wrote on last edited by
    #1

    Hi, i am trying to build a project which requests a data from website ,parse document, and store them in a table.

    void MainWindow::on_pushButton_clicked()
    {
    QNetworkAccessManager *mana = new QNetworkAccessManager(this);
        QString url ="https://openapi.naver.com/v1/search/news.json?query=";
    
    
        QString keyword = ui->lineEdit->text();
    
        url += keyword;
    
        QNetworkRequest req;
        req.setUrl (QUrl(url));
        req.setRawHeader("X-Naver-Client-Id", id);
        req.setRawHeader("X-Naver-Client-Secret", pwd);
        QNetworkReply *reply = mana->get(req);
        qDebug() << reply->readAll();
    }
    

    Here is my code so far to check if anything came back.

    googled a lot, saw similar codes to what i wrote but couldn't make it return anything other than empty string.

    what am i doing wrong and how can i fix it? or any exemplary code for my purpose will be appreciated.

    eyllanescE 1 Reply Last reply
    0
    • S sodu

      Hi, i am trying to build a project which requests a data from website ,parse document, and store them in a table.

      void MainWindow::on_pushButton_clicked()
      {
      QNetworkAccessManager *mana = new QNetworkAccessManager(this);
          QString url ="https://openapi.naver.com/v1/search/news.json?query=";
      
      
          QString keyword = ui->lineEdit->text();
      
          url += keyword;
      
          QNetworkRequest req;
          req.setUrl (QUrl(url));
          req.setRawHeader("X-Naver-Client-Id", id);
          req.setRawHeader("X-Naver-Client-Secret", pwd);
          QNetworkReply *reply = mana->get(req);
          qDebug() << reply->readAll();
      }
      

      Here is my code so far to check if anything came back.

      googled a lot, saw similar codes to what i wrote but couldn't make it return anything other than empty string.

      what am i doing wrong and how can i fix it? or any exemplary code for my purpose will be appreciated.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @sodu Qt is asynchronous so you must use the signals to get the result:

      QNetworkAccessManager *mana = new QNetworkAccessManager(this);
      connect(mana, &QNetworkAccessManager::finished, this, &MainWindow::handleFinished);
      
      void MainWindow::handleFinished(QNetworkReply *reply){
          qDebug() << reply->readAll();
          reply->deleteLater();
      }
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      3
      • jeremy_kJ Online
        jeremy_kJ Online
        jeremy_k
        wrote on last edited by
        #3

        Another option is to block on QIODevice::waitForReadyRead, followed by read or readAll if true was returned.

        The readyRead or finished signals are usually the better option, presuming there is an event loop running anyway. The documentation contains this curious note for the QAbstractSocket::waitForReadyRead override that is worth considering:
        This function may fail randomly on Windows. Consider using the event loop and the readyRead() signal if your software will run on Windows.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        Pablo J. RoginaP 1 Reply Last reply
        1
        • jeremy_kJ jeremy_k

          Another option is to block on QIODevice::waitForReadyRead, followed by read or readAll if true was returned.

          The readyRead or finished signals are usually the better option, presuming there is an event loop running anyway. The documentation contains this curious note for the QAbstractSocket::waitForReadyRead override that is worth considering:
          This function may fail randomly on Windows. Consider using the event loop and the readyRead() signal if your software will run on Windows.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @sodu

          Another option is to block on QIODevice::waitForReadyRead

          I'd suggest not to follow this way, please take a look at @eyllanesc mention working asynchronously with signals/slots...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sodu
            wrote on last edited by
            #5

            wow.. I've tried connect() before and used wrong slot. now i know why.

            thank you!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved