In this exercise, the app simple embed a WebView with Javascript enabled, loading local HTML page in /assets/mypage.html, to parse the feed of my blog (http://feeds.feedburner.com/Android-er).
package com.exercise.AndroidGoogleFeed;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class AndroidGoogleFeedActivity extends Activity {
WebView myWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
myWebView = new WebView(this);
setContentView(myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/mypage.html");
}
}
/assets/mypage.html
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("http://feeds.feedburner.com/Android-er");
feed.setNumEntries(10);
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var div = document.createElement("div");
var A = document.createElement("A");
A.setAttribute("href",entry.link);
A.appendChild(document.createTextNode(entry.title));
div.appendChild(A);
container.appendChild(div);
}
}
});
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="feed"></div>
</body>
</html>
"android.permission.INTERNET" is needed in AndroidManifest.xml.
Download the files.
next:
- Display Google Feed in mobile style, using jQuery Mobile.
0 Response to "Using Google Feed API in embedded local HTML"
Post a Comment