How to access JSON web services from QML



  • Translated from Spanish with Google

    I would like to know what is needed to access JSON web data, specifically to the API of
    Community Radio Browser: http://www.radio-browser.info/webservice

    For example, this call:
    Http://www.radio-browser.info/webservice/json/stations/bytag/soul

    Until now I had the following example of access to the Flickr API of the book Qt 5 Cadaques (https://qmlbook.github.io/en/ch11/index.html#flickr-calls), but it does not work for other services:

    import QtQuick 2.5

    Rectangle {
    width: 320
    height: 480
    ListView {
    id: view
    anchors.fill: parent
    delegate: Thumbnail {
    width: view.width
    text: modelData.title
    iconSource: modelData.media.m
    }
    }

    function request() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
                print('HEADERS_RECEIVED')
            } else if(xhr.readyState === XMLHttpRequest.DONE) {
                print('DONE')
                var json = JSON.parse(xhr.responseText.toString())
                view.model = json.items
            }
        }
        xhr.open("GET", "http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=munich");
        xhr.send();
    }
    
    Component.onCompleted: {
        request()
    }
    

    }

    Does it depend on whether the JSON response returns a callback or not?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.