Access a REST Service

You can easily connect to REST services using the XMLHttpRequest object.

There is an in-depth guide how to create a weather app that uses a weather REST service posted on our blog: How to Access REST Services with Qt and V-Play: Weather Service Example App

 import VPlayApps 1.0
 import QtQuick 2.0

 App {
   // This signal handler is called when the app is created, like a constructor
   Component.onCompleted: getIp()

   NavigationStack {
     Page {
       AppText {
         id: ipText
         anchors.centerIn: parent
       }
     }
   }

   function getIp() {
     // Create the XMLHttpRequest object
     var xhr = new XMLHttpRequest

     // Listen to the readyStateChanged signal
     xhr.onreadystatechange = function() {
       // If the state changed to DONE, we can parse the response
       if (xhr.readyState === XMLHttpRequest.DONE) {
         // The responseText looks like this {"ip":"xxx.xxx.xxx.xxx"}
         // Parse the responseText string to JSON format
         var responseJSON = JSON.parse(xhr.responseText)
         // Read the ip property of the response
         var ip = responseJSON.ip
         // Display the ip in the AppText item
         ipText.text = "IP: " + ip
       }
     }

     // Define the target of your request
     xhr.open("GET", "https://api.ipify.org?format=json")
     // Execute the request
     xhr.send()
   }
 }

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