Forums

OverviewFelgo 2 Support (Qt 5) › Infinite loop when doing multiple HttpRequest

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #23355

    Pierre

    Hi,

    I am very new to QML and programming in general. I am trying to build a private app for my home automation system.

     

    I built an HTTP REST client to fetch data from an API. It works pefectly for one request, but when I use the client function twice on the same page, it keeps recalculating both functions endlessly…

     

    Here is my client code:

    QtObject {
            id: _
            readonly property string apiUrl: "XXX"
            readonly property string apiKey: "XXX"
            property string jsonData: "null"
            property string value: "null"
            //property string collectdate: "null"
    
            function post(method, cmdid) {
                HttpRequest.post(apiUrl)
                .timeout(maxRequestTimeout)
                .set('Content-Type', 'application/json')
                .send({
                          "jsonrpc": "2.0",
                          "method": method,
                          "params": {"id":[cmdid],"apikey": apiKey}
                     })
                .end(function(err, res) {
                    if(res.ok) {
    
                    jsonData = JSON.stringify(res.body.result)
    
                    var keys = Object.keys(res.body.result)
                    //console.log("keys= " + keys)
    
                    var keyid = keys [Object.keys(keys)[0]]
                    //console.log("keyid= " + keyid)
    
                    value = JSON.stringify(res.body.result[keyid].value)
                    //console.log("valeur= " + value);
    
                    var collectdate = JSON.stringify(res.body.result[keyid].collectDate)
                    //console.log("date de collecte= " + collectdate);
    
                    //return value
    
                    }
                    else {
                    console.log(err.message)
                    console.log(err.resonse)
                    }
                }
                )
                return value
             }
       }
    
        function execCmd(cmdid) {
            console.log(_.post("methodtype", cmdid))
            return _.post("methodtype", cmdid)
        }
    }

     

    And my page code:

     

    API{
            id: api
        }
    
    
        AppText {
            text: "Salon : " + api.execCmd(#CMDID_1#) + "°C"
            anchors.centerIn: parent
        }
    
        AppText {
            text: "Bureau : " + api.execCmd(#CMDID_2#) + "°C"
        }

     

    So if I have only one AppText (one CMD_ID), it works fine. But with two CMD_ID, I have an infinite loop on both console and display.

     

    I also tried with Promise, same result…

     

    I have the feeling that I am missing a very simple programming concept…

     

    Thanks a lot for your help
    Pierre

Viewing 1 post (of 1 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support