Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using QML the send post XML-requests
Forum Updated to NodeBB v4.3 + New Features

Using QML the send post XML-requests

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 11.7k Views 1 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.
  • P Offline
    P Offline
    pixtur
    wrote on 12 Nov 2010, 07:50 last edited by
    #1

    Hi,

    I'm trying to write a small QML/JavaScript app. For this, I wanted to send XML-requests to an online API.
    How could I send the following XML-request vis post-method from QML?

    @
    <?xml version="1.0" encoding="UTF-8" ?>
    <CreateSessionRequest >
    <accesskey>Your API-Key</accesskey>
    <vendor>Your Vendor-ID/Scout-ID</vendor>
    </CreateSessionRequest>
    @

    And how could I parse the XML-response and (potentially) convert it into a json-like javascript object?

    thanks!
    /tom

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on 14 Nov 2010, 23:53 last edited by
      #2

      Hi,

      You should be able to do this with "XMLHttpRequest":http://doc.qt.nokia.com/4.7/qdeclarativeglobalobject.html#xmlhttprequest

      Regards,
      Michael

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cmer4
        wrote on 31 May 2011, 09:38 last edited by
        #3

        I have been checking the way XmlHttpRequest works and here is the code that is sending an xml request to remote server:

        @onClicked: {
        var doc = new XMLHttpRequest();
        var postData = "xml=<common><version>1.0</version></common>";
        doc.open("POST", "http://api.service.net/api", true);
        doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        doc.onreadystatechange = function() {
        if (doc.readyState == doc.DONE) {
        console.log("Your data has been sent");
        }
        if (doc.status == 200, doc.readyState == 4) {
        var resp = doc.responseXML
        console.log("Doc XML response ready")
        }
        else console.log("hmmm?")
        }
        doc.send(postData);
        }@

        Now basically I should be able to parse the response using XmlListModel? but how do I use that?
        If I put the XmlListModel after the code above it will be outside of javascript and won't work...Could someone advise me?

        And ofc we will make the pose SOLVED together as I answered (I guess) - 50% of the original question;))

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cmer4
          wrote on 31 May 2011, 10:58 last edited by
          #4

          @var doc = new XMLHttpRequest();
          var postData = "<common><version>1.0</version><api_key>XXX==</api_key></common>";
          doc.open("POST", "http://api.service.net/api", true)
          doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
          doc.send(postData)
          doc.onreadystatechange = function() {
          if (doc.readyState == 4) {
          console.log("Your data has been sent")
          if (doc.status == 200) {console.log("URL exists")}}
          else console.log("hmm")@

          My digging continues - as the server replies with 200 I guess I should be getting the reply now. But it takes super brain to understand for me (I am not a programmer;( ) what is the next step to parse it...as I need the data to be available to my application seems like I need to stick to XmlHttpRequest and try to use its parsing capabilities? but then is there a method to make the value extracted - available to the app?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cmer4
            wrote on 31 May 2011, 14:46 last edited by
            #5

            I almost give up now, but some progress done...I learned the way ajax works and hence tried to apply the logic. I used russian Yandex.locator api for testing (like Google latitude)...

            So I am able to actually get response in xml and browse through the nodes, but whatever I do nodeValue is not working for me returning "undefinded" - BUG?

            @onClicked: {
            var doc = new XMLHttpRequest();
            var postData = "xml=<ya_lbs_request><common><version>1.0</version><api_key>AN8H5E0BAAAAz4biJAUARzyc-Zh1KKgasTzFatJuqzsT-xkAAAAAAAAAAACUROPR814LedbZKMmgp84RFNv2TQ==</api_key></common><gsm_cells><cell><countrycode>250</countrycode><operatorid>1</operatorid><cellid>2294</cellid><lac>6405</lac><signal_strength>-45</signal_strength><age>1000</age></cell></gsm_cells></ya_lbs_request>";
            doc.open("POST", "http://api.lbs.yandex.net/geolocation", true)
            doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
            doc.send(postData)
            doc.onreadystatechange = function() {
            if (doc.readyState == 4) {
            console.log("Your data has been sent")
            if (doc.status == 200) {console.log("URL exists");
            var root = doc.responseXML.documentElement;
            console.log(root.childNodes[1].childNodes[1].name)
            }}
            else console.log("hmm")
            }
            }@

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cmer4
              wrote on 31 May 2011, 15:02 last edited by
              #6

              "link":http://www.w3schools.com/dom/dom_nodes_get.asp

              works now)

              So response I am parsing is as follows:

              @<?xml version="1.0" encoding="UTF-8"?>
              <ya_lbs_response>
              <position>
              <latitude>55.7351526</latitude>
              <longitude>37.5951196</longitude>
              <altitude>0.0</altitude>
              <precision>826.319232</precision>
              <altitude_precision>30.0</altitude_precision>
              <type>gsm</type>
              </position>
              </ya_lbs_response>@

              @{
              var doc = new XMLHttpRequest();
              var postData = "xml=<ya_lbs_request><common><version>1.0</version><api_key>AN8H5E0BAAAAz4biJAUARzyc-Zh1KKgasTzFatJuqzsT-xkAAAAAAAAAAACUROPR814LedbZKMmgp84RFNv2TQ==</api_key></common><gsm_cells><cell><countrycode>250</countrycode><operatorid>1</operatorid><cellid>2294</cellid><lac>6405</lac><signal_strength>-45</signal_strength><age>1000</age></cell></gsm_cells></ya_lbs_request>";
              doc.open("POST", "http://api.lbs.yandex.net/geolocation", true)
              doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
              doc.send(postData)
              doc.onreadystatechange = function() {
              if (doc.readyState == 4) {
              console.log("Your data has been sent")
              if (doc.status == 200) {console.log("URL exists");
              var root = doc.responseXML.documentElement;
              console.log(root.childNodes[1].childNodes[1].childNodes[0].nodeValue)
              }}
              else console.log("hmm")
              }
              }@

              Worth reading this: "LINK":http://www.w3schools.com/dom/dom_nodes_get.asp

              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