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. Synchronous QT HTTP GET for Json parsing
QtWS25 Last Chance

Synchronous QT HTTP GET for Json parsing

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.1k Views
  • 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.
  • R Offline
    R Offline
    rackover
    wrote on last edited by
    #1

    Hi,
    I need my application to communicate with a server that sends data as JSON.
    The data should not exceed a few kb.
    Here is how it works :
    Initial situation : I have a record ID. I want to download this record.
    Step 1 : I ask the server about this record, using something like http://api.com/data/records?id=<my ID>.
    Step 2 : The server display a json page which holds data about this record, containing the download URL of the record
    Step 3 : My app parses the JSON data to find the download Url. After that, my app downloads the file asynchronously.

    I am now currently at step 2.
    I need to HTTP GET this data synchronously and in a blocking way. I dont need to store the record JSON data on disk or anything, I just want to GET it and to parse it in a single function without having to use the SIGNAL/SLOT system.

    All the info I find on the internet about using Qt GET is either asynchronous, or overkill.
    How should I do it ?

    aha_1980A 1 Reply Last reply
    0
    • R rackover

      Hi,
      I need my application to communicate with a server that sends data as JSON.
      The data should not exceed a few kb.
      Here is how it works :
      Initial situation : I have a record ID. I want to download this record.
      Step 1 : I ask the server about this record, using something like http://api.com/data/records?id=<my ID>.
      Step 2 : The server display a json page which holds data about this record, containing the download URL of the record
      Step 3 : My app parses the JSON data to find the download Url. After that, my app downloads the file asynchronously.

      I am now currently at step 2.
      I need to HTTP GET this data synchronously and in a blocking way. I dont need to store the record JSON data on disk or anything, I just want to GET it and to parse it in a single function without having to use the SIGNAL/SLOT system.

      All the info I find on the internet about using Qt GET is either asynchronous, or overkill.
      How should I do it ?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @rackover use signals and slots and parse the received data in the slot.

      I hear a lot that people want a blocking solution and most often they can use the non-blocking without problems.

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • R Offline
        R Offline
        rackover
        wrote on last edited by rackover
        #3

        I'm in a for loop, as I will need to get multiple records information. If I start using signals and slot, I will need to await them all to be finished, and that will add another layer of complexity over an otherwise very simple need. Also, having to wait for something to complete before executing further code, means that it will be blocking anyway. So there's no point in making it asynchronous at all.

        I need the whole "download URLs fetching" to be held in a single function.

        aha_1980A 1 Reply Last reply
        0
        • R rackover

          I'm in a for loop, as I will need to get multiple records information. If I start using signals and slot, I will need to await them all to be finished, and that will add another layer of complexity over an otherwise very simple need. Also, having to wait for something to complete before executing further code, means that it will be blocking anyway. So there's no point in making it asynchronous at all.

          I need the whole "download URLs fetching" to be held in a single function.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rackover then you will block Qt's event loop and your program will not work as expected, if at all.

          the only way for blocking operations is in a separate thread.

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rackover
            wrote on last edited by
            #5

            What do you mean "it won't work as expected, if at all" ? And by "blocking operations in a separate thread" ?

            Could you elaborate a bit ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Qt's application uses an event loop, if you put a while loop in you are application, you'll block the event loop and thus your application.

              Why would it be more complicated to use the asynchronous nature of Qt ? From the looks of it, each step has to wait on the previous one. Number one and two looks like a call to QNetworkAccessManager::get while number three the content of the slot connected to the finished signal.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • R Offline
                R Offline
                rackover
                wrote on last edited by
                #7

                This is a very simple application / tool. It doesn't have GUI at all, it is not supposed to be responsive or fluid by any way. The fact that the application freezes or hangs during download isn't a big deal at all.

                If I use the finished signal, will I still be able to make the whole 1) 2) + json parsing operations in a single function ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  With a lambda you can keep everything pretty tidy.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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