2015-08-03 63 views
1

在我的項目中,我使用的是sails.js。從test1.ejs我調用一個Web服務,然後使用res.view()調用另一個ejs(test2.ejs)。 現在android用戶正在輸入一些影響數據庫的值,需要實時反映在網頁上。我無法弄清楚如何使用sails.js來實現。 此外,我需要甚至顯示android用戶響應,同時刷新網頁。總之,我想要一個像股市這樣的動態UI,其中服務器上的任何變化都反映在前端。 我需要使用其他任何類似angularjs的東西嗎?在sails.js中實時刷新網頁

回答

2

如果我理解你的問題,你可以使用JavaScript接口。

您應該創建類是這樣的:

public class WebAppInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c) { 
     mContext = c; 
    } 

    /** Show a toast from the web page */ 
    @JavascriptInterface 
    public void showToast(String toast) { 
     Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
    } 
} 

這之後,您應在該接口連接到您的WebView這樣的:

webView.addJavascriptInterface(new WebAppInterface(this), "Android"); 

現在你可以從JS調用Java這樣的代碼:

<script type="text/javascript"> 
    function showAndroidToast(toast) { 
     Android.showToast(toast); 
    } 
</script> 

或者像這樣調用Java的JS代碼:

webview.loadUrl("javascript:window.showAndroidToast(\"Hello, World!\")"); 

更多信息,請訪問:https://developer.android.com/guide/webapps/webview.html

+0

日Thnx的答覆,但我面臨的問題是如何自動刷新網頁。移動應用程序也可以被另一個網頁取代。 – KshitishSC

+0

哦...對不起...我沒有看標籤,想過Android! –