Skip to content

Instantly share code, notes, and snippets.

@paulb777
Created November 3, 2011 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulb777/1335562 to your computer and use it in GitHub Desktop.
Save paulb777/1335562 to your computer and use it in GitHub Desktop.
Twitter AJAX API call from PhoneGap
<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Ajax Sample</title>
<script type="text/javascript" src="phonegap-1.1.0.js"></script>
<script type="text/javascript">
function appReady(){
var ajax = new XMLHttpRequest();
ajax.open("GET","http://search.twitter.com/search.json?q=phonegap",true);
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState===4 && (ajax.status===200||ajax.status===0)){
// alert(JSON.stringify(ajax));
var data = JSON.parse(ajax.responseText);
var theResults = data.results;
var theHTML = '';
for(var i=0;i<theResults.length;i++){
theHTML += ['<div class="tweet"',
'<div class="avatar"> <img src='+theResults[i].profile_image_url+' /></div>',
'<div class="tweet_content">',
'<h2>'+theResults[i].from_user+'</h2>',
'<p>'+theResults[i].text+'</p>',
'</div>',
'</div>'].join('');
}
document.getElementById('main').innerHTML = theHTML;
}
};
}
document.addEventListener("deviceready", appReady, false);
</script>
<style type="text/css">
.tweet {padding-bottom:5px;}
.avatar {float: left; height: 48px; width: 48px;}
.tweet_content {margin-left: 60px; min-height: 48px;}
</style>
</head>
<body>
<div id="main">
</div>
</body>
</html>
@paulb777
Copy link
Author

paulb777 commented Nov 3, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment