Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Blackhawk
Roku Guru

Re: Install error

I have been working on this code to connect to my site


Sub RunUserInterface()
    screen = CreateObject("roSGScreen")
    scene = screen.CreateScene("HomeScene")
    port = CreateObject("roMessagePort")
    screen.SetMessagePort(port)
    screen.Show()
   
    oneRow = GetApiArray()
    list = [
        {
            ContentList : oneRow
        }
    ]
    scene.gridContent = ParseXMLContent(list)
    while true
        msg = wait(0, port)
        print "------------------"
        print "msg = "; msg
    end while
   
    if screen <> invalid then
        screen.Close()
        screen = invalid
    end if
End Sub

Function ParseXMLContent(list As Object)
    RowItems = createObject("RoSGNode","ContentNode")
   
    for each rowAA in list
    'for index = 0 to 1
        row = createObject("RoSGNode","ContentNode")
        row.Title = rowAA.Title
        for each itemAA in rowAA.ContentList
            item = createObject("RoSGNode","ContentNode")
            ' We don't use item.setFields(itemAA) as doesn't cast streamFormat to proper value
            for each key in itemAA
                item[key] = itemAA[key]
            end for
            row.appendChild(item)
        end for
        RowItems.appendChild(row)
    end for
    return RowItems
End Function

Function GetApiArray()
    url = CreateObject("roUrlTransfer")
    url.SetUrl("http://griffingatv.website/xml/categories.xml")
    rsp = url.GetToString()
    responseXML = ParseXML(rsp)
    responseXML = responseXML.GetChildElements()
    responseArray = responseXML.GetChildElements()
    result = []
    for each xmlItem in responseArray
        if xmlItem.getName() = "item"
            itemAA = xmlItem.GetChildElements()
            if itemAA <> invalid
                item = {}
                for each xmlItem in itemAA
                    item[xmlItem.getName()] = xmlItem.getText()
                    if xmlItem.getName() = "media:content"
                        item.stream = {url : xmlItem.url}
                        item.url = xmlItem.getAttributes().url
                        item.streamFormat = "mp4"
                       
                        mediaContent = xmlItem.GetChildElements()
                        for each mediaContentItem in mediaContent
                            if mediaContentItem.getName() = "media:thumbnail"
                                item.HDPosterUrl = mediaContentItem.getattributes().url
                                item.hdBackgroundImageUrl = mediaContentItem.getattributes().url
                            end if
                        end for
                    end if
                end for
                result.push(item)
            end if
        end if
    end for
    return result
End Function

Function ParseXML(str As String) As dynamic
    if str = invalid return invalid
    xml=CreateObject("roXMLElement")
    if not xml.Parse(str) return invalid
    return xml
End Function



There's an error, How can I fix this?
'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &
hec) in pkg:/source/main.brs(60)
060:     responseXML = responseXML.GetChildElements()
0 Kudos
Komag
Roku Guru

Re: Install error

    responseXML = ParseXML(rsp)
    responseXML = responseXML.GetChildElements()
    responseArray = responseXML.GetChildElements()


This section looks odd to me, but I don't have experience with this.
0 Kudos
destruk
Binge Watcher

Re: Install error

If it can't parse the xml then it will fail there.
I would suggest putting a stop command before the
responsexml=parsexml(rsp)

Then in the debugger when it stops print rsp
If something shows up then the rsp is ok - and single step the lines until you see the issue.
Ideally for debugging you wouldn't want to replace the variable you are working with or a failure will lose data, so -
responseXML = ParseXML(rsp)
    responseXML2 = responseXML.GetChildElements()
    responseArray = responseXML2.GetChildElements()

It's trying to pick out the data two levels deep of children.  A better laid-out xml feed would make it easier to deal with, but without control of the feed this is one way to do it.
0 Kudos
belltown
Roku Guru

Re: Install error

You're going to have a tough time making any progress if you're not checking for errors. Your code just blindly assumes that everything just works until it eventually crashes.

For example, the following GetString() code will fail to return any XML because it can't access your web site at the url you specified (the nameservers aren't set up):
url.SetUrl("http://.../categories.xml")
rsp = url.GetToString()


At the very least, you need to check that the rsp value is non-blank before attempting to parse the result, and print some kind of error message to the debug console.

Then you're calling:
responseXML = ParseXML(rsp)

again, without checking whether or not it parsed (ParseXML returns invalid), which it won't because you're just passing in an empty string from the previous GetToString() call.

In general, any time a function call can return something you're not expecting and that your subsequent code is not set up to handle (invalid, empty string, etc.) you should check for that at the time, otherwise your program may stumble on for several more statements before it finally crashes, and you'll have a really hard time figuring out where the problem originated.
0 Kudos