Corona SDK: How To Get a User’s Last Tweet

Corona is mainly a game development framework but it has some components that allow you to make other types apps and since it’s it is built in Lua you can use any Lua library out there. In this post I will show you how to use HTTP requests and JSON to read a twitter user’s latest tweet and display on the screen.

The Twitter API

The method we will be using from the twitter API is GET statuses/user_timeline, this method returns several components from a tweet including a user’s profile picture, user name and the tweet of course.

Our app will display the user’s profile picture, username and last tweet.

mobiledevtuts' last tweet as seen in the simulator

the latest tweet app

Requesting The Twitter Feed

The parameters of the URL where you request data from will be screen_name and count where screen_name is the user’s user name and count is the number of tweets you want to receive.

"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mobiledevtuts&count=2"

The function http.request will be used to get the JSON data feed from twitter’s API. This function returns four things: the json data which include’s all the tweet’s information, http status, response headers and response status line.

This is how you make the call.

local http = require "socket.http"
local data,httpStatus,httpResponseHeaders,httpResponseStatusLine=http.request("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mobiledevtuts&count=2")

If you want you can show the contents of these variables like this

print("Data: "..data)
print("HTTP Status: "..httpStatus)
httpResponseHeaders=json.encode(httpResponseHeaders)
print("HTTP Response Headers: "..httpResponseHeaders)
print("HTTP Response Status Line: "..httpResponseStatusLine)

Notice that the data at this point is a string, we are going to have to encode it so we can use it as an array.

JSON and Twitter Feeds

If this is your first time using JSON the way you have to think about JSON is as if it was just an array of data. Most programming languages have functions that convert JSON data to arrays which work just like any other arrays of the programming language.

This is how we encode the json string to a json array in Lua:

local tweet=json.decode(data)

Now all the tweets, two in this case since that is the value of our count parameter in the URL, are stored as an array in the tweet variable. Remember that array keys in Lua start at one and not zero, so to refer to the latest tweet we use

tweet[1].parameter

where parameter is the data we want to display from the tweet.

tweet[1].user.profile_image_url -- this gives us the user's profile image url
tweet[1].user.name -- the user name
tweet[1].text -- the tweet's text

The Full Code

Putting it all together this is what we need.

local json = require "json"
local http = require "socket.http"

local data,httpStatus,httpResponseHeaders,httpResponseStatusLine=http.request("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mobiledevtuts&count=2")

--[[ uncomment for debugging purposes
print("Data: "..data)
print("HTTP Status: "..httpStatus)

httpResponseHeaders=json.encode(httpResponseHeaders)
print("HTTP Response Headers: "..httpResponseHeaders)
print("HTTP Response Status Line: "..httpResponseStatusLine)
]]--

local background=display.newRect(0,0,320,480)
background:setFillColor(0xff,0xff,0xff)

local tweet=json.decode(data)
local profilePicture
local userName
local tweetText

local function listener( event )
        if ( event.isError ) then
                print ( "Network error - download failed" )
        else


        end
end


if data ~= nil then
	profilePicture=display.loadRemoteImage(tweet[1].user.profile_image_url,"GET",listener,"userPhoto.png",10,150)
	userName=display.newText("User: \n"..tweet[1].user.name,10,200):setTextColor(0,0,0)
	tweetText=display.newText("Tweet: \n"..tweet[1].text,10,250):setTextColor(0,0,0)
end



Related posts: