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: 
ib_beat
Visitor

Communicating with Web Service issue

So, I'm new in the whole Brightscript/Roku development environment and I'm having an issue with a request of a webservice. For ease, I made a function in the main.brs that I can call from other scripts that basically creates a roUrlTransfer object, gets the contents of the url in a variable and returns it. Here's the code:
Function vGetUrlContent(p_url As String)
    url = CreateObject("roUrlTransfer")
    url.SetUrl(p_url)
    html = url.GetToString()
    Return html
End Function


The same code (kinda) is already used successfully in the same main.brs file that has another purpose, but the same 3 lines are used.
However, when I call this function I get this error here:

BRIGHTSCRIPT: ERROR: roUrlTransfer: creating MAIN|TASK-only component failed on RENDER thread: pkg:/source/main.brs(306)

BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Suspending threads...
Thread selected:  1*   pkg:/source/main.brs(307)               url.SetUrl(p_url)

Current Function:
305:  Function vGetUrlContent(p_url As String)
306:      url = CreateObject("roUrlTransfer")
307:*     url.SetUrl(p_url)
308:      html = url.GetToString()
309:      Return html
310:  End Function
'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in pkg:/source/main.brs(307)
307:     url.SetUrl(p_url)
Backtrace:
#1  Function vgeturlcontent(p_url As String) As Dynamic
   file/line: pkg:/source/main.brs(307)
#0  Function onitemselected() As Void
   file/line: pkg:/components/screens/DetailsScreen/DetailsScreen.brs(84)
Local Variables:
p_url            roString (2.1 was String) refcnt=1 val:"(HERE IS THE ACTUAL WEBSERVICE URL)"
global           Interface:ifGlobal
m                roAssociativeArray refcnt=3 count:6
url              Invalid
html             <uninitialized>
Threads:
ID    Location                                Source Code
 0    pkg:/source/main.brs(80)                msg = wait(0, port)
 1*   pkg:/source/main.brs(307)               url.SetUrl(p_url)
  *selected

Brightscript Debugger>


Any ideas?
0 Kudos
3 REPLIES 3
joetesta
Roku Guru

Re: Communicating with Web Service issue

you can't do http requests from render thread. 
https://sdkdocs.roku.com/display/sdkdoc/Task
aspiring
0 Kudos
ib_beat
Visitor

Re: Communicating with Web Service issue

How can I get around that if I have to call an http request from a render thread, if I can't call a main.brs function that does it for me?
0 Kudos
tim_beynart
Channel Surfer

Re: Communicating with Web Service issue

you create an instance of your task in the render thread logic, then run it.
for example,  you could write a task called "http_request" 
in the code for a screen (which is in the render thread), you do something like this:
  task = CreateObject("roSGNode", "http_request")
task.observeField("response","someFunctionThatHandlesResponses")
  task.url = url
  task.control = "RUN"
0 Kudos