I have been trying to implement this combination for some time and I am making some headway. I would love some feedback though. I don’t know if this is the ideal way to do it as it seems to be quite slow and it still not finished.
What I aim to do is implement johnbeebe FFL but receiving JSON file instead of showing it as and HTML popup. There are great advantages to this, since you can then post the data as you wish in your app as well as using it in other various scenarios to compare your score with your friends. This is not possible with the HTML popup since you can’t manipulate the results. This is what I have so far.
local http = require("socket.http")
local ltn12 = require("ltn12")
local facebook = require "facebook"
function toJson()
--the url needed a forward slash ( / ) after unicornlabs.com
myUrl = url2
--native.setActivityIndicator(true)
local path = system.pathForFile( "mos.content", system.DocumentsDirectory )
local myFile = io.open( path, "w+b" )
--Check for a connection to the host
r, c, h = http.request{
method = "HEAD",
url = myUrl
}
local hostFound = false
if c == 200 then
print("host found!")
hostFound = true
else
print("host not found")
end
--If a connection with the host was found, get the content
if hostFound then
http.request{
url = myUrl,
sink = ltn12.sink.file(myFile)
-- sink = ltn12.sink.file(io.stdout)
}
--Open the file containing the content we received
local file = io.open(path, "r")
local content = file:read("\*a")
--Show the content in the terminal
print(content)
--Show the content
native.showAlert(content,content) -- this is just for testing would be removed
t = Json.Decode(content) -- this is to decode the JSON file and store it into a table
native.cancelWebPopup() -- closes the webpopup
end
end
-- listener for "fbconnect" events
local function listener( event )
if ( "session" == event.type ) then
-- upon successful login, request list of friends of the signed in user
if ( "login" == event.phase ) then
--facebook.request( "me/friends" )
FFLJson()
end
elseif ( "request" == event.type ) then
-- event.response is a JSON object from the FB server
local response = event.response
-- if request succeeds, create a scrolling list of friend names
if ( not event.isError ) then
response = json.decode( event.response )
local data = response.data
for i=1,#data do
local name = data[i].name
print( name )
end
end
elseif ( "dialog" == event.type ) then
print( "dialog", event.response )
end
end
-- NOTE: You must provide a valid application id provided from Facebook
local appId = "FacebookAPPID"
if ( appId ) then
facebook.login( appId, listener )
else
local function onComplete( event )
system.openURL( "http://developers.facebook.com/setup" )
end
native.showAlert( "Error", "To develop for Facebook Connect, you need to get an application id from Facebook's website.", { "Learn More" }, onComplete )
end
function FFLJson ()
local webListener = function( event )
local shouldLoad = true
url2 = event.url
if 1 == string.find( url2, "corona:close" ) then
-- Close the web popup
shouldLoad = false
elseif string.find(url2, "{", 1, false) \< 10 then -- trying to determine if it's the JSON string, not currently succesful
toJson()-- if it is the JSON string go to this function
else
end
return shouldLoad
end
local showRanksUrl = "http://yourdomain.com/loadfriendranks/?bestscore=" .. bestScore
native.showWebPopup( showRanksUrl, { urlRequest=webListener } )
end
Some code explanation. First bear in mind that I have close to none PHP experience. I was able to modify johnbeebe php code to return the JSON string with the scores. This is really easy as you just echo the table generate from his code.
So I initially open a webpopup that calls johnbeebe’s code. Instead of outputting the HTML it outputs JSON. When the URL gets returned from the webpopup, it checks if it’s a JSON string, if it is it goes to the http.request where it decodes the file and stores it into a table. You have to go to an http.request since the URL that you initially go to is not the one that gets returned with the JSON file.
I am open for any suggestion.
Thanks
[import]uid: 8192 topic_id: 5080 reply_id: 305080[/import]