xmlrokubrightscriptscenegraph

Making a Get request in brightscript for roku channel not working


I am building a Roku tv channel, I have displayed a keyboard so user can type, and there are two buttons to search, I have made an observer on these buttons, when the button is pressed, I want to make a get request to get results, but the API call is not succeeding in components/keyboard.brs which is linked with components/keyboard.xml, but this same API call succeed if I call in source/main.brs on the top. Here you can see I have made observer on buttons and then calling another function

fetchResults function which is called from searchImages and searchFeed functions on button clicked

Can't understand this, please let me know if you know something about that, or any resource where can I find these things. Thanks in advance.


Solution

  • All http requests need to be performed on a task thread.

    You can see a working example of this on Roku's github samples repository here.

    Here's the snippet of task code from that sample:

    <?xml version = "1.0" encoding = "utf-8" ?>
    
    <!--********** Copyright 2016 Roku Corp.  All Rights Reserved. **********-->
    
    <component name = "ContentReader" extends = "Task" >
    
      <interface>
        <field id = "contenturi" type = "uri" />
        <field id = "content" type = "node" />
      </interface>
    
      <script type = "text/brightscript" >
    
        <![CDATA[
    
        sub init()
          m.top.functionName = "getcontent"
        end sub
    
        sub getcontent()
          content = createObject("roSGNode", "ContentNode")
    
          contentxml = createObject("roXMLElement")
    
          readInternet = createObject("roUrlTransfer")
          readInternet.setUrl(m.top.contenturi)
          contentxml.parse(readInternet.GetToString())
    
          if contentxml.getName()="Content"
            for each item in contentxml.GetNamedElements("item")
              itemcontent = content.createChild("ContentNode")
              itemcontent.setFields(item.getAttributes())
            end for
          end if
    
          m.top.content = content
        end sub
    
        ]]>
    
      </script>
    
    </component>
    

    And this is how you create and start the task:

    m.readXMLContentTask = createObject("roSGNode", "ContentReader")
    m.readXMLContentTask.observeField("content", "setcontent")
    m.readXMLContentTask.contenturi = "http://www.sdktestinglab.com/Tutorial/content/xmlcontent.xml"
    m.readXMLContentTask.control = "RUN"
    
    
    

    You can see a slightly more complex example of http requests in a task here on the RokuCommunity sample-projects repo: https://github.com/rokucommunity/sample-projects/tree/master/standard-with-task