Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dbulli
Level 7

PostFromString Array??

Is there anyway to post an array to server?


events[0][ev]:event1
events[0][ts]:1374269099
events[0][pos]:0
events[1][ev]:event2
events[1][ts]:1374269100
events[1][pos]:0
events[2][ev]:event3
events[2][ts]:1374269100
events[2][pos]:0
- Daniel
http://dbulli.com
0 Kudos
5 Replies
RokuJoel
Roku Employee
Roku Employee

Re: PostFromString Array??

Well, you can post an AA (associative array) to a server by treating it as an array of headers using roURLTransfer.setheaders(AA).

Or you could convert the whole thing to a string of name=value&name=value&name=value pairs and post that.

Not sure if you intend to post each element in events at once, or each AA within events separately, in which case the first approach could work.

- Joel
0 Kudos
dbulli
Level 7

Re: PostFromString Array??

Thanks Joel, the back end server is php and when I print $_POST i don't see anything being passed. The backend would have to be rewritten to look on the HEADERS.

Every time I have tried to encode the string, then I get an error? I would like to iterate over the events on the server side like this:

foreach($_POST['events'] as $event){
echo $event . ' ';
}
- Daniel
http://dbulli.com
0 Kudos
TheEndless
Level 9

Re: PostFromString Array??

You have to serialize the data somehow, either to the querystring or to the post body (url encoded name/value pairs, json, or xml). How would you submit the data via a website or another app? The same would be true in BrightScript.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
dbulli
Level 7

Re: PostFromString Array?? [SOLVED]

Solved like this:


postString = ""
ander =""
pos% = 0
for each event in events
eventString = "events["+AnyToString(pos%)+"][ev]="+AnyToString(event["ev"])
eventString = eventString+"&events["+AnyToString(pos%)+"][ts]="+AnyToString(event["ts"])
eventString = eventString+"&events["+AnyToString(pos%)+"][pos]="+AnyToString(event["pos"])
postString = postString + ander + eventString
ander = "&"
pos% = pos% + 1
end for

xfer.PostFromString(postString)

'******************************************************
'HELPERS
'******************************************************


'******************************************************
'itostr
'
'Convert int to string. This is necessary because
'the builtin Stri(x) prepends whitespace
'******************************************************
Function itostr(i As Integer) As String
str = Stri(i)
return strTrim(str)
End Function

'******************************************************
'Trim a string
'******************************************************
Function strTrim(str As String) As String
st=CreateObject("roString")
st.SetString(str)
return st.Trim()
End Function

'try to convert an object to a string. return invalid if can't
'******************************************************
Function AnyToString(any As Dynamic) As dynamic
if any = invalid return "invalid"
if isstr(any) return strTrim(any)
if isint(any) return itostr(any)
if isbool(any)
if any = true return "true"
return "false"
endif
if isfloat(any) return Str(any)
if type(any) = "roTimespan" return itostr(any.TotalMilliseconds()) + "ms"
return invalid
End Function
- Daniel
http://dbulli.com
0 Kudos
dbulli
Level 7

Re: PostFromString Array??

"TheEndless" wrote:
You have to serialize the data somehow, either to the querystring or to the post body (url encoded name/value pairs, json, or xml). How would you submit the data via a website or another app? The same would be true in BrightScript.


Endless, solved but HTML forms handle this automatically. THanks for taking a look.
- Daniel
http://dbulli.com
0 Kudos