hi, i'm currently working on a project that require me to read from several servlet/feed. i am able to read from one servlet but am facing problem reading from the second servlet.
below are my codes (i only uses one .js file for the project and serveral .js files to process the data retrieved from the servlet):
///////////////////////////////////////////////////////////////////////////////
// RSS Reader example widget that demonstrates use of the WRTKit UI toolkit
// in a real-world widget.
// Feed update frequency.
var updateFrequencyOptions = [
{ value: -1, text: "never" },
{ value: (1000 * 60 * 5), text: "every 5 min" },
{ value: (1000 * 60 * 15), text: "every 15 min" },
{ value: (1000 * 60 * 60), text: "every 60 min" },
];
// Feed update broker.
var feedUpdateBroker = null;
var feedUpdateBroker2 = null;
// Reference to current feed items controls.
var feedItemControls = [];
var feedItemControls2 = [];
// Feed item control pool.
var feedItemControlPool = [];
var feedItemControlPool2 = [];
// Time when the feed was last modified.
var feedLastModified = null;
// Flag that tracks if a feed update is commanded or automatic.
var feedUpdateCommanded = false;
var feedUpdateCommanded2 = false;
// Reference to the WRTKit user interface manager and views.
var uiManager;
var mainView;
var settingsView;
var currentView;
// breakfast
var breakfastView;
var bfMainDishView;
var bfIngredientsView;
//Reference to main view controls
var BreakfastButton;
//Reference to breakfast view controls
var BfMainDishButton;
var BfIngredientsButton;
// Reference to settings controls.
var feedNameText;
var feedName2Text;
var feedUrlText;
var feedUrl2Text;
var eventLabel;
var feedSelection;
var feedUpdateFrequencySelection;
var settingsSaveButton;
var settingsCancelButton;
// Constants for menu item identifiers.
var MENU_ITEM_SETTINGS = 0;
var MENU_ITEM_REFRESH = 1;
var MENU_ITEM_ABOUT = 2;
// Feed update timer identifier.
var updateTimerId = null;
//About menu lable
var aboutLabel;
// Feed name, URL and update frequency (in milliseconds; -1 if no auto update).
var feedName = "Meal Planner";
//var feedURL = "http://sitconnect.nyp.edu.sg/citizenryWidgetServer/latestNews";
var feedURL = "http://172.20.45.45:8080/FYPJ_Meal_Planner/BreakfastServlet";
//Ingredients
var feedName2 = "Ingredients";
var feedURL2 = "http://172.20.45.45:8080/FYPJ_Meal_Planner/breakfastIngredients";
var feedUpdateFrequency = -1;
// Next scheduled update time; -1 if never.
var feedUpdateTime = -1;
var currentViewIndex = -1;
// Called from the onload event handler to initialize the widget.
function init() {
if (window.widget) {
// set tab-navigation mode and show softkeys
widget.setNavigationEnabled(false);
window.menu.showSoftkeys();
// create menu
var settingsMenuItem = new MenuItem("Settings", MENU_ITEM_SETTINGS);
settingsMenuItem.onSelect = menuItemSelected;
menu.append(settingsMenuItem);
var refreshMenuItem = new MenuItem("Refresh", MENU_ITEM_REFRESH);
refreshMenuItem.onSelect = menuItemSelected;
menu.append(refreshMenuItem);
var aboutMenuItem = new MenuItem("About", MENU_ITEM_ABOUT);
aboutMenuItem.onSelect = menuItemSelected;
menu.append(aboutMenuItem);
}
// load preferences
loadPreferences();
// create UI manager
uiManager = new UIManager();
// create main view
mainView = new ListView();
// create settings view
settingsView = new ListView(null, "Settings");
//Create about view
aboutView = new ListView(null, "RSS Reader");
//create breakfast view
breakfastView = new ListView(null, "Breakfast");
//create bfMainDish view
bfMainDishView = new ListView(null, "MainDish");
//create bfIngredientsView
bfIngredientsView = new ListView(null, "Ingredients");
//add button to main view
BreakfastButton = new FormButton(null,"Breakfast");
BreakfastButton.addEventListener("ActionPerformed", showbreakfastView);
mainView.addControl(BreakfastButton);
//add button to breakfast view
BfMainDishButton = new FormButton(null,"MainDish");
BfMainDishButton.addEventListener("ActionPerformed", showbfMainDishView);
breakfastView.addControl(BfMainDishButton);
BfIngredientsButton = new FormButton(null, "Ingredients");
BfIngredientsButton.addEventListener("ActionPerformed", showbfIngredientsView);
breakfastView.addControl(BfIngredientsButton);
// feed name control
feedNameText = new TextField('feedNameText', "Feed name", feedName);
settingsView.addControl(feedNameText);
feedName2Text = new TextField('feedNameText2', "Feed name", feedName2);
settingsView.addControl(feedName2Text);
// feed url control
feedUrlText = new TextField('feedUrlText', "Feed URL", feedURL);
settingsView.addControl(feedUrlText);
feedUrl2Text = new TextField('feedUrl2Text', "FeedURL", feedURL2);
settingsView.addControl(feedUrl2Text);
// About lable control
aboutLabel = new Label();
aboutView.addControl(aboutLabel);
// feed update frequency selection control
feedUpdateFrequencySelection = new SelectionList(null, "Check for updates", updateFrequencyOptions);
settingsView.addControl(feedUpdateFrequencySelection);
// save settings button
settingsSaveButton = new FormButton(null, "Save");
settingsSaveButton.addEventListener("ActionPerformed", saveSettingsClicked);
settingsView.addControl(settingsSaveButton);
// cancel settings button
settingsCancelButton = new FormButton(null, "Cancel");
settingsCancelButton.addEventListener("ActionPerformed", showMainView);
settingsView.addControl(settingsCancelButton);
// display the main view if a feed has been configured
// otherwise show the settings view
if (feedURL != null) {
showMainView();
// updateFeed();
}
else {
showSettings();
}
if (feedURL2 != null) {
showMainView();
updateFeed();
} else {
showSettings();
}
// start feed update timer (called once every second)
updateTimerId = setInterval(updateFeedTimerFunc, 1000);
updateTimerId = setInterval(updateFeedTimerFunc2, 1000);
}
// Callback for when menu items are selected.
function menuItemSelected(id) {
switch (id) {
case MENU_ITEM_SETTINGS:
showSettings();
break;
case MENU_ITEM_REFRESH:
updateFeed();
break;
case MENU_ITEM_ABOUT:
showAboutView();
break;
}
}
// Loads widget preferences.
function loadPreferences(){
if (window.widget) {
// read feed URL, name and update frequency from the widget settings
if (typeof widget.preferenceForKey("FeedURL") != 'undefined' && typeof widget.preferenceForKey("FeedName") != 'undefined') {
feedURL = widget.preferenceForKey("FeedURL");
feedName = widget.preferenceForKey("FeedName");
feedURL2 = widget.preferenceForKey("FeedURL");
feedName2 = widget.preferenceForKey("FeedName");
var feedUpdateFrequencyStr = widget.preferenceForKey("FeedUpdateFrequency");
feedUpdateFrequency = (feedUpdateFrequencyStr == null) ? -1 : parseInt(feedUpdateFrequencyStr);
}
}
}
// Loads widget preferences.
function savePreferences() {
if (window.widget) {
// save settings in widget preferences store
widget.setPreferenceForKey(feedURL, "FeedURL");
widget.setPreferenceForKey(feedName, "FeedName");
widget.setPreferenceForKey(feedURL2, "FeedURL2");
widget.setPreferenceForKey(feedName2, "FeedName2");
widget.setPreferenceForKey(feedUpdateFrequency.toString(), "FeedUpdateFrequency");
}
}
// Callback for settings view save button.
function saveSettingsClicked() {
// remember old URL
var oldURL = feedURL;
// update feed name and URL
var selectedFeed = feedUrlText.getText();
if (selectedFeed != null) {
feedURL = selectedFeed;
feedName = feedNameText.getText();
} else {
feedURL = null;
feedName = null;
}
// update frequency
var selectedFrequency = feedUpdateFrequencySelection.getSelected();
feedUpdateFrequency = (selectedFrequency != null) ? selectedFrequency.value : -1;
// save preferences
savePreferences();
// return to main view
showMainView();
function saveSettingsClicked() {
// remember old URL
var oldURL = feedURL2;
// update feed name and URL
var selectedFeed2 = feedUrl2Text.getText();
if (selectedFeed2 != null) {
feedURL2 = selectedFeed2;
feedName2 = feedName2Text.getText();
} else {
feedURL2 = null;
feedName2 = null;
}
// update frequency
var selectedFrequency2 = feedUpdateFrequencySelection2.getSelected2();
feedUpdateFrequency2 = (selectedFrequency2 != null) ? selectedFrequency2.value : -1;
// save preferences
savePreferences();
// return to main view
showMainView();
// update the feed if the feed URL has changed
if (feedURL != oldURL) {
feedLastModified = null;
removeFeedItems();
updateFeed();
}
}
// update the feed if the feed URL has changed
if (feedURL2 != oldURL) {
feedLastModified = null;
removeFeedItems2();
updateFeed2();
}
}
// Show main view.
function showMainView(){
// set main view caption from feed name
var mainViewCaption = (feedName == null) ? null : feedName;
mainView.setCaption(mainViewCaption);
// set right softkey to "exit"
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
// show the main view
uiManager.setView(mainView);
}
//show brakfast view
function showbreakfastView(){
// set main view caption from feed name
var breakfastViewCaption = (feedName == null) ? null : feedName;
breakfastView.setCaption(breakfastViewCaption);
// set right softkey to "exit"
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
// show the main view
uiManager.setView(breakfastView);
}
//show bfMainDishView
function showbfMainDishView(){
// set main view caption from feed name
var bfMainDishViewCaption = (feedName == null) ? null : feedName;
bfMainDishView.setCaption(bfMainDishViewCaption);
// set right softkey to "exit"
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
// show the main view
uiManager.setView(bfMainDishView);
updateFeedTimerFunc();
}
//show bf ingredients view
function showbfIngredientsView(){
// set main view caption from feed name
var bfIngredientsViewCaption = (feedName2 == null) ? null : feedName2;
bfIngredientsView.setCaption(bfIngredientsViewCaption);
// set right softkey to "exit"
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
// show the main view
uiManager.setView(bfIngredientsViewCaption);
updateFeedTimerFunc2();
}
// Show settings view.
function showSettings() {
// Feed Name
feedNameText.setText(feedName);
feedName2Text.setText(feedName2);
// URL
feedUrlText.setText(feedURL);
feedUrl2Text.setText(feedUrl2);
// frequency
var feedUpdateFrequencyOption = feedUpdateFrequencySelection.getOptionForValue(feedUpdateFrequency);
feedUpdateFrequencySelection.setSelected(feedUpdateFrequencyOption);
if (feedURL == null) {
// no valid configuration
// disable cancel button - set right softkey to "exit"
settingsCancelButton.setEnabled(false);
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
} else {
// we have a valid configuration
// enable cancel button - set right softkey to "cancel"
settingsCancelButton.setEnabled(true);
if (window.widget) {
menu.setRightSoftkeyLabel("Cancel", showMainView);
}
}
if (feedURL2 == null) {
// no valid configuration
// disable cancel button - set right softkey to "exit"
settingsCancelButton.setEnabled(false);
if (window.widget) {
menu.setRightSoftkeyLabel("", null);
}
} else {
// we have a valid configuration
// enable cancel button - set right softkey to "cancel"
settingsCancelButton.setEnabled(true);
if (window.widget) {
menu.setRightSoftkeyLabel("Cancel", showMainView);
}
}
// show the settings view
uiManager.setView(settingsView);
}
//Displays the About view
function showAboutView(){
aboutLabel.setText("This Widget includes software licensed from Nokia © 2008");
setAboutViewSoftkeys();
uiManager.setView(aboutView);
}
// Sets the softkeys for about view.
function setAboutViewSoftkeys(){
if (window.widget) {
// set right softkey to "Ok" (returns to main view)
menu.setRightSoftkeyLabel("Ok", showMainView);
}
}
// Schedules an immediate feed update.
function updateFeed(){
feedUpdateTime = 0;
feedUpdateCommanded = true;
}
function updateFeed2(){
feedUpdateTime2 = 0;
feedUpdateCommanded2 = 0;
}
// Timer function for feed updates - called once every second.
function updateFeedTimerFunc(){
var now = new Date().getTime();
// check if a feed update has been scheduled, if it's time to update now,
// and if there's no update currently in progress and if we're in the main view
if ((feedURL != null) &&
(feedUpdateTime != -1) &&
(now > feedUpdateTime) &&
(feedUpdateBroker == null) &&
(uiManager.getView() == mainView)) {
// show progress dialog if this is a commanded feed update
if (feedUpdateCommanded) {
// no auto hiding, wait-type notification, unknown progress
uiManager.showNotification(-1, "wait", "Loading feed...", -1);
}
// fetch the feed from the specified URL
feedUpdateBroker = new FeedUpdateBroker();
feedUpdateBroker.fetchFeed(feedURL, feedUpdateCompleted);
if (feedUpdateFrequency != -1) {
// schedule next update
feedUpdateTime = now + feedUpdateFrequency;
}
else {
// feed update frequency is "never"
feedUpdateTime = -1;
}
}
}
function updateFeedTimerFunc2(){
var now = new Date().getTime();
// check if a feed update has been scheduled, if it's time to update now,
// and if there's no update currently in progress and if we're in the main view
if ((feedURL2 != null) &&
(feedUpdateTime != -1) &&
(now > feedUpdateTime) &&
(feedUpdateBroker2 == null) &&
(uiManager.getView() == bfIngredientsView)) {
// show progress dialog if this is a commanded feed update
if (feedUpdateCommanded2) {
// no auto hiding, wait-type notification, unknown progress
uiManager.showNotification(-1, "wait", "Loading feed...", -1);
}
// fetch the feed from the specified URL
feedUpdateBroker2 = new FeedUpdateBroker2();
feedUpdateBroker2.fetchFeed(feedURL2, feedUpdateCompleted2);
if (feedUpdateFrequency != -1) {
// schedule next update
feedUpdateTime = now + feedUpdateFrequency;
}
else {
// feed update frequency is "never"
feedUpdateTime = -1;
}
}
}
// Callback function that gets called when a feed update has completed.
function feedUpdateCompleted(event){
if (event.status == "ok") {
// if there aren't any feed items yet, we'll hide the progress dialog
if (feedUpdateCommanded) {
uiManager.hideNotification();
setFeedItems(event.item);
feedItemControls[0].setFocused(true);
}
}
function feedUpdateCompleted2(event){
if (event.status == "ok") {
// if there aren't any feed items yet, we'll hide the progress dialog
if (feedUpdateCommanded2) {
uiManager.hideNotification();
if (uiManager.getView() == bfIngredientsView) {
setFeedItems2(event.item);
feedItemControls2[0].setFocused(true);
}
}
}
else {
// show error message
uiManager.showNotification(3000, "warning", "Error while updating feed!<br/>(check network settings)");
}
// null the broker reference to indicate that there's no current
// update in progress
feedUpdateBroker2 = null;
// reset commanded feed update flag
feedUpdateCommanded2 = false;
}
}
// Removes feed items.
function removeFeedItems(){
// remove all current feed items from the main view
for (var i = 0; i < feedItemControls.length; i++) {
bfMainDishView.removeControl(feedItemControls[i]);
}
// reset feed item control array
feedItemControls = [];
}
// Removes feed items.
function removeFeedItems2(){
// remove all current feed items from the main view
for (var i = 0; i < feedItemControls2.length; i++) {
bfIngredientsView.removeControl(feedItemControls2[i]);
}
// reset feed item control array
feedItemControls2 = [];
}
// Sets feed items.
function setFeedItems(items){
// start by removing all current feed items
removeFeedItems();
// create new feed items and add them to the main view
// use feed item pool to recycle controls
for (var i = 0; i < items.length; i++) {
// get a feed item control from the pool or create one and
// place it in the pool if there aren't enough feed item controls
var feedItemControl;
if (i == feedItemControlPool.length) {
feedItemControl = new ContentPanel(null, true);
feedItemControlPool.push(feedItemControl);
}
else {
feedItemControl = feedItemControlPool[i];
}
// initialize feed item control
var item = items[i];
feedItemControl.setCaption("");
feedItemControl.setContent(getContentHTMLForFeedItem(item));
feedItemControl.setExpanded(false);
// add the feed item control to the main view
feedItemControls.push(feedItemControl);
bfMainDishView.addControl(feedItemControl);
}
}
function setFeedItems2(items){
// start by removing all current feed items
removeFeedItems2();
// create new feed items and add them to the main view
// use feed item pool to recycle controls
for (var i = 0; i < items.length; i++) {
// get a feed item control from the pool or create one and
// place it in the pool if there aren't enough feed item controls
var feedItemControl2;
if (i == feedItemControlPool2.length) {
feedItemControl2 = new ContentPanel(null, true);
feedItemControlPool2.push(feedItemControl2);
}
else {
feedItemControl2 = feedItemControlPool2[i];
}
// initialize feed item control
var item2 = item2s[i];
feedItemControl2.setCaption("");
feedItemControl2.setContent(getContentHTML2ForFeedItem(item));
feedItemControl2.setExpanded(false);
// add the feed item control to the main view
feedItemControls2.push(feedItemControl2);
bfIngredientsView.addControl(feedItemControl2);
}
}
// Returns the content HTML for a feed item.
function getContentHTMLForFeedItem(item){
var buf = "";
// item date
if (item.name != null) {
buf += "<div>" + "Name:" + item.name + "</div>";
}
/*if (item.ingredients != null){
buf += "<div>" + "Ingredients: " + item.ingredients + "</div>";
}
if(item.grocerylist != null){
buf += "<div>" + "Grocery List: " + item.grocerylist +"</div>";
}
if(item.instructions != null){
buf += "<div>" + "Instructions: " + item.instructions +"</div>";
}
if(item.calories != null){
buf += "<div>" + "Total Calories: " + item.calories +"</div>";
}
if(item.cholesterol != null){
buf += "<div>" + "Total Cholesterol:: " + item.cholesterol +"</div>";
}
if(item.sodium != null){
buf += "<div>" + "Total Sodium: " + item.sodium +"</div>";
}
if(item.carbohydrate != null){
buf += "<div>" + "Total Carbohydrate: " + item.carbohydrate +"</div>";
}
if(item.protein != null){
buf += "<div>" + "Total Protein: " + item.protein +"</div>";
}
if(item.fat != null){
buf += "<div>" + "Total Fat: " + item.fat +"</div>";
}
*/
return buf;
}
function getContentHTML2ForFeedItem(item2){
var buf = "";
// item date
if (item2.ingredients != null) {
buf += "<div>" + "Ingredients: " + item2.ingredients + "</div>";
}
/*if(item.grocerylist != null){
buf += "<div>" + "Grocery List: " + item.grocerylist +"</div>";
}
if(item.instructions != null){
buf += "<div>" + "Instructions: " + item.instructions +"</div>";
}
if(item.calories != null){
buf += "<div>" + "Total Calories: " + item.calories +"</div>";
}
if(item.cholesterol != null){
buf += "<div>" + "Total Cholesterol:: " + item.cholesterol +"</div>";
}
if(item.sodium != null){
buf += "<div>" + "Total Sodium: " + item.sodium +"</div>";
}
if(item.carbohydrate != null){
buf += "<div>" + "Total Carbohydrate: " + item.carbohydrate +"</div>";
}
if(item.protein != null){
buf += "<div>" + "Total Protein: " + item.protein +"</div>";
}
if(item.fat != null){
buf += "<div>" + "Total Fat: " + item.fat +"</div>";
}
*/
return buf;
}
// Opens a URL.
function openURL(url){
if (window.widget) {
// in WRT
widget.openURL(url);
}
else {
// outside WRT
window.open(url, "NewWindow");
}
}
is there any error in my codes. because i keep getting error stating that the feedURL2 is not defined and is pointing error to the WRTKit uiManager.js.