Sample For Retrieving News

If anyone knows how, can you put a sample up that illustrates grabbing news for the app that has been posted using the cloud interface.

I have my user interface set up so that the user clicks on News and a window pops up to display the news items.

I haven’t been successful in hacking at it until it works, so a simple sample would help me out a lot.

Corona, your example docs don’t need to be masterpieces… please release more guides

Got it working*ish.

I’m using widgetcandy, but you should be able to get the idea from what I’ve got here.

I’ve been able to access the ._id, and .title so far.  I’ll keep digging, hopping that .content and .image are the other two parts, but there has to be a date stored as well in there.

[lua]

local newsView = function ()

                    print(“newsView called”)

                    

                    --EVENT LISTENER

                    function ccNews( event )

                        if ( event.type == “News” ) then 

                            print( “Get News successful” )

                            --event.results = table of values related to scores

                            local results = event.results

                            local maxNews = #results

                            if ( maxNews > 3 ) then maxNews = 3 end  --get maximum of 3 news items

                            local yOffset = 20

                            

                            for i = 1, maxNews do

                                local text = results[i].title – results[i]._id

                                   

                                _G.GUI.NewLabel(

                                        {

                                        x               = “5%”,

                                        y               = yOffset,

                                        scale           = _G.GUIScale,

                                        name            = “newsItem” … i,

                                        theme           = “theme_3”,

                                        parentGroup     = “newsWin”,

                                        width           = “90%”,

                                        caption         = text,

                                        textAlign       = “left”,

                                        } ) 

                                yOffset = yOffset + 50

                                    

                            end  

                        end

                    end

                    Runtime:addEventListener( “News”, ccNews )

                        

                    – CREATE A WINDOW

                    _G.GUI.NewWindow(

                                {

                                x               = “center”,

                                y               = “center”,

                                scale           = _G.GUIScale,

                                parentGroup     = nil,

                                width           = “95%”,

                                height          = “95%”,

                                theme           = “theme_3”,

                                name            = “newsWin”,

                                caption         = “Game News”,

                                textAlign       = “center”,

                                modal           = true,

                                shadow          = true,

                                closeButton     = true,

                                dragX           = false,

                                dragY           = false,

                                onClose         = function(EventData) 

                                                    – clear news window

                                                    

                                                    _G.GUI.GetHandle(“newsWin”):destroy()

                                                    end,

                                } )

                    – Get the news

                    coronaCloud.getNews()

                end

[/lua]

Try something like this though I haven’t tested it.  You can add this to a for loop easily to get all stories, though I have focussed it on the 1st news story.  To access the image filename for your news story you need to use .image_file_name

local function shownews(stories) local newsdash = display.newRect(50,200,200,100) newsdash.newstitle= {}     newsdash.newstitle[1]= display.newText(stories[1].title,60,220,180,0,native.systemFont,14)     newsdash.newstitle[1]:setTextColor(0,0,128)     newsdash.newsstory= {}     newsdash.newsstory[1]= display.newText(stories[1].text,60,240,180,0,native.systemFont,12)     newsdash.newsstory[1]:setTextColor(0)     local dateString = stories[1].updated\_at     local pattern = "(%d+)%-(%d+)%-(%d+)"     local year, month, day = dateString:match( pattern )     newsdash.newstime = display.newText(month.."/"..day.."/"..year,60,205,180,0,native.systemFont,10)     newsdash.newstime:setTextColor(0) end   local function newsconnect ( event)     if event.type == "News" then         local newsresultstable = event.results         shownews(newsresultstable)     end end Runtime:addEventListener( "News", newsconnect)

Hey icyspark,

we must have been posting at the same time  ;)

the code I posted is working for me

so .content does not give you the content of the news item…

Haha yes, though hopefully you can get the extra content ie date and image name from my post

It’s .text for the content and .updated_at or .created_at for the date/time of post

yep, that’s working now

ty

Also for getting all the info you need set

coronaCloudController.debugEnabled = true

in the corona-cloud-core.lua

It prints out to the console all the properties etc, so it will make your life much easier

that sounds great,

but, I haven’t been able to log in using the simulator.

I have to keep building and sending it to a device to test, which leaves me with zilch for feedback.

Maybe I’m doing something wrong.

Can I hard code the login creds and the simulator will log on, I thought I got a network error when I tried something like that last week.

On windows system if that matters, probably does…

I’ll give it a shot tomorrow

err…

that worked fine

ye I just hardcoded my email and password, then ran

coronaCloud.loginAPI( email, password)

so you can test on simulator

Got it working*ish.

I’m using widgetcandy, but you should be able to get the idea from what I’ve got here.

I’ve been able to access the ._id, and .title so far.  I’ll keep digging, hopping that .content and .image are the other two parts, but there has to be a date stored as well in there.

[lua]

local newsView = function ()

                    print(“newsView called”)

                    

                    --EVENT LISTENER

                    function ccNews( event )

                        if ( event.type == “News” ) then 

                            print( “Get News successful” )

                            --event.results = table of values related to scores

                            local results = event.results

                            local maxNews = #results

                            if ( maxNews > 3 ) then maxNews = 3 end  --get maximum of 3 news items

                            local yOffset = 20

                            

                            for i = 1, maxNews do

                                local text = results[i].title – results[i]._id

                                   

                                _G.GUI.NewLabel(

                                        {

                                        x               = “5%”,

                                        y               = yOffset,

                                        scale           = _G.GUIScale,

                                        name            = “newsItem” … i,

                                        theme           = “theme_3”,

                                        parentGroup     = “newsWin”,

                                        width           = “90%”,

                                        caption         = text,

                                        textAlign       = “left”,

                                        } ) 

                                yOffset = yOffset + 50

                                    

                            end  

                        end

                    end

                    Runtime:addEventListener( “News”, ccNews )

                        

                    – CREATE A WINDOW

                    _G.GUI.NewWindow(

                                {

                                x               = “center”,

                                y               = “center”,

                                scale           = _G.GUIScale,

                                parentGroup     = nil,

                                width           = “95%”,

                                height          = “95%”,

                                theme           = “theme_3”,

                                name            = “newsWin”,

                                caption         = “Game News”,

                                textAlign       = “center”,

                                modal           = true,

                                shadow          = true,

                                closeButton     = true,

                                dragX           = false,

                                dragY           = false,

                                onClose         = function(EventData) 

                                                    – clear news window

                                                    

                                                    _G.GUI.GetHandle(“newsWin”):destroy()

                                                    end,

                                } )

                    – Get the news

                    coronaCloud.getNews()

                end

[/lua]

Try something like this though I haven’t tested it.  You can add this to a for loop easily to get all stories, though I have focussed it on the 1st news story.  To access the image filename for your news story you need to use .image_file_name

local function shownews(stories) local newsdash = display.newRect(50,200,200,100) newsdash.newstitle= {}     newsdash.newstitle[1]= display.newText(stories[1].title,60,220,180,0,native.systemFont,14)     newsdash.newstitle[1]:setTextColor(0,0,128)     newsdash.newsstory= {}     newsdash.newsstory[1]= display.newText(stories[1].text,60,240,180,0,native.systemFont,12)     newsdash.newsstory[1]:setTextColor(0)     local dateString = stories[1].updated\_at     local pattern = "(%d+)%-(%d+)%-(%d+)"     local year, month, day = dateString:match( pattern )     newsdash.newstime = display.newText(month.."/"..day.."/"..year,60,205,180,0,native.systemFont,10)     newsdash.newstime:setTextColor(0) end   local function newsconnect ( event)     if event.type == "News" then         local newsresultstable = event.results         shownews(newsresultstable)     end end Runtime:addEventListener( "News", newsconnect)

Hey icyspark,

we must have been posting at the same time  ;)

the code I posted is working for me

so .content does not give you the content of the news item…

Haha yes, though hopefully you can get the extra content ie date and image name from my post

It’s .text for the content and .updated_at or .created_at for the date/time of post

yep, that’s working now

ty

Also for getting all the info you need set

coronaCloudController.debugEnabled = true

in the corona-cloud-core.lua

It prints out to the console all the properties etc, so it will make your life much easier