#1  
Old 03-19-2012, 01:02 PM
kengan kengan is offline
Member
 
Join Date: Nov 2011
Posts: 34
Thanked: 14 times
Default Http Request in QML (N9)

Hi, I trying to write a Harmattan app to search and display lyrics from Chartlyrics.com using QML. The problem is that QML doesn't support SOAP protocol to request data from server. The other protocol supported is Http GET. QML has a XMLHttpRequest() function but the syntax doesn't seem to match Http GET protocol. Is this the function I should be using? Can somebody guide me on the syntax? The documentation and example for this function is very skimpy.

This is the Chartlyric API website: http://www.chartlyrics.com/api.aspx
and protocol example: http://api.chartlyrics.com/apiv1.asmx?op=SearchLyric

Last edited by kengan; 03-20-2012 at 01:57 PM.
Reply With Quote
  #2  
Old 03-23-2012, 12:39 AM
nicolai's Avatar
nicolai nicolai is offline
Senior Member
 
Join Date: Feb 2010
Posts: 124
Thanked: 134 times
Default Re: Http Request in QML (N9)

Hi Kengan,

here is a simple example how to parse the XML-Response:
Code:
import QtQuick 1.1

Rectangle {
    width: 360
    height: 360
    function parseSoundResponse(lyricsNode) {
        var lyricsItemData = {}
        for(var ii = 1;ii<lyricsNode.length;ii+=2)
        {
            var resource = lyricsNode[ii].childNodes
            var o = new Object()
            for(var ik = 1;ik<resource.length;ik+=2)
            {
                if(resource[ik].nodeName === "Artist")
                {
                    lyricsItemData.artist = resource[ik].firstChild.nodeValue
                }
                if(resource[ik].nodeName === "Song")
                {
                    lyricsItemData.song = resource[ik].firstChild.nodeValue
                }
                if(resource[ik].nodeName === "SongUrl")
                {
                    lyricsItemData.song_url = resource[ik].firstChild.nodeValue
                }
            }
            if(lyricsItemData.artist)
                lyricsResultListModel.append(lyricsItemData)
        }
    }

    function doRequest(artist, song) {
        lyricsResultListModel.clear()
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
                    if (doc.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
                    } else if (doc.readyState === XMLHttpRequest.DONE) {
                        var a = doc.responseXML.documentElement;
                        parseSoundResponse(a.childNodes)
                    }
                }
        doc.open("GET", "http://api.chartlyrics.com/apiv1.asmx/SearchLyric?artist="+artist+"&song="+song)
        doc.send()
    }

    ListModel {
        id:lyricsResultListModel
    }

    Rectangle {
        id:searchBox
        width:parent.width
        height:64
        anchors.top:parent.top
        color:"darkgreen"
        border.width:2
        border.color:"green"

        Row {
            spacing:4
            anchors.margins: 4
            anchors.centerIn: parent
            Rectangle {
                width:searchBox.width/3
                height:48
                radius:16
                TextInput {
                    text:"Artist"
                    id:artistInput
                    font.pixelSize: 24
                    width:parent.width
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            Rectangle {
                width:searchBox.width/3
                height:48
                radius:16
                TextInput {
                    text:"Song"
                    id:songInput
                    font.pixelSize: 24
                    width:parent.width
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            Rectangle {
                radius:16
                color:"gray"
                border.width:1
                border.color:"darkgray"
                width:searchLabel.width
                height:48
                Text {
                    id:searchLabel
                    anchors.centerIn: parent
                    font.pixelSize: 24
                    text:"search"
                }
                MouseArea {
                    anchors.fill:parent
                    onClicked: {
                        doRequest(artistInput.text, songInput.text)
                    }
                }
            }
        }
    }

    ListView {
        clip:true
        width:parent.width
        anchors.bottom:parent.bottom
        anchors.top:searchBox.bottom
        model:lyricsResultListModel
        delegate: Item {
            width:parent.width
            height:2*64
            Rectangle { border.width:1;border.color:"black"
                anchors.fill:parent
                Column {
                    Text { text:"<b>" + model.artist+"<b>"}
                    Text { text:"<a href=\"" +model.song_url+"\">"+model.song+"</a>";textFormat:Text.RichText
                        onLinkActivated: {
                            Qt.openUrlExternally(link)
                        }
                    }
                }
            }
        }
    }
}
Reply With Quote
  #3  
Old 03-23-2012, 02:43 PM
kengan kengan is offline
Member
 
Join Date: Nov 2011
Posts: 34
Thanked: 14 times
Default Re: Http Request in QML (N9)

Thanks nicolai, your code will be very useful. I still have to find out how to request for the response in the first.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Request] SMS/IM in status bar st11 Handset 2 10-13-2011 05:45 AM
Qt HTTP POST request with Meego 1.2 Harmattan meandnano Handset 0 09-28-2011 10:10 AM
http://wiki.meego.com/SDK/Tablet/VirtualBox janiemel Tablet 3 07-14-2011 06:42 AM
http://apollo.jf.intel.com/ always unreachable? monoceros Application Developer Support 3 04-19-2011 04:05 PM
Tag request thread texrat Community Matters 17 05-10-2010 09:32 AM


All times are GMT. The time now is 10:32 PM.