0

WebページのJavaスクリプトに存在する関数をandroid appに呼び出す方法。この関数によって返された結果をandroid app内に表示します。WebアプリケーションのJavaスクリプトに存在する関数を呼び出す

たとえば、getTalukas関数はStringsのリストを返します。アンドロイドアプリに表示します。

<script type="text/javascript"> 
//<![CDATA[ 
var PageMethods = function() { 
PageMethods.initializeBase(this); 
this._timeout = 0; 
this._userContext = null; 
this._succeeded = null; 
this._failed = null; 
} 
PageMethods.prototype = { 
_get_path:function() { 
var p = this.get_path(); 
if (p) return p; 
else return PageMethods._staticInstance.get_path();}, 
getTalukas:function(id,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getTalukas',false,{id:id},succeededCallback,failedCallback,userContext); }, 
getVillages:function(did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getVillages',false,{did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
getKnos:function(ptxt,vid,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getKnos',false,{ptxt:ptxt,vid:vid,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
getSnos:function(ptxt,vid,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getSnos',false,{ptxt:ptxt,vid:vid,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
getKnames:function(ptxt,vid,opt,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getKnames',false,{ptxt:ptxt,vid:vid,opt:opt,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
getSnames:function(ptxt,vid,opt,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getSnames',false,{ptxt:ptxt,vid:vid,opt:opt,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
call8a:function(kno,vid,dn,tn,vn,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'call8a',false,{kno:kno,vid:vid,dn:dn,tn:tn,vn:vn,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
call712:function(sno,vid,dn,tn,vn,tc,dc,did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'call712',false,{sno:sno,vid:vid,dn:dn,tn:tn,vn:vn,tc:tc,dc:dc,did:did,tid:tid},succeededCallback,failedCallback,userContext); }, 
getCTSOs:function(id,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getCTSOs',false,{id:id},succeededCallback,failedCallback,userContext); }, 
getCTSOLink:function(did,tid,succeededCallback, failedCallback, userContext) { 
return this._invoke(this._get_path(), 'getCTSOLink',false,{did:did,tid:tid},succeededCallback,failedCallback,userContext); }} 
PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy); 
PageMethods._staticInstance = new PageMethods(); 
PageMethods.set_path = function(value) { PageMethods._staticInstance.set_path(value); } 
PageMethods.get_path = function() { return PageMethods._staticInstance.get_path(); } 
PageMethods.set_timeout = function(value) { PageMethods._staticInstance.set_timeout(value); } 
PageMethods.get_timeout = function() { return PageMethods._staticInstance.get_timeout(); } 
PageMethods.set_defaultUserContext = function(value) { PageMethods._staticInstance.set_defaultUserContext(value); } 
PageMethods.get_defaultUserContext = function() { return PageMethods._staticInstance.get_defaultUserContext(); } 
PageMethods.set_defaultSucceededCallback = function(value) { PageMethods._staticInstance.set_defaultSucceededCallback(value); } 
PageMethods.get_defaultSucceededCallback = function() { return PageMethods._staticInstance.get_defaultSucceededCallback(); } 
PageMethods.set_defaultFailedCallback = function(value) { PageMethods._staticInstance.set_defaultFailedCallback(value); } 
PageMethods.get_defaultFailedCallback = function() { return PageMethods._staticInstance.get_defaultFailedCallback(); } 
PageMethods.set_enableJsonp = function(value) { PageMethods._staticInstance.set_enableJsonp(value); } 
PageMethods.get_enableJsonp = function() { return PageMethods._staticInstance.get_enableJsonp(); } 
PageMethods.set_jsonpCallbackParameter = function(value) { PageMethods._staticInstance.set_jsonpCallbackParameter(value); } 
PageMethods.get_jsonpCallbackParameter = function() { return PageMethods._staticInstance.get_jsonpCallbackParameter(); } 
PageMethods.set_path("Home.aspx"); 
PageMethods.getTalukas= function(id,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getTalukas(id,onSuccess,onFailed,userContext); } 
PageMethods.getVillages= function(did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getVillages(did,tid,onSuccess,onFailed,userContext); } 
PageMethods.getKnos= function(ptxt,vid,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getKnos(ptxt,vid,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.getSnos= function(ptxt,vid,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getSnos(ptxt,vid,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.getKnames= function(ptxt,vid,opt,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getKnames(ptxt,vid,opt,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.getSnames= function(ptxt,vid,opt,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getSnames(ptxt,vid,opt,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.call8a= function(kno,vid,dn,tn,vn,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.call8a(kno,vid,dn,tn,vn,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.call712= function(sno,vid,dn,tn,vn,tc,dc,did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.call712(sno,vid,dn,tn,vn,tc,dc,did,tid,onSuccess,onFailed,userContext); } 
PageMethods.getCTSOs= function(id,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getCTSOs(id,onSuccess,onFailed,userContext); } 
PageMethods.getCTSOLink= function(did,tid,onSuccess,onFailed,userContext) {PageMethods._staticInstance.getCTSOLink(did,tid,onSuccess,onFailed,userContext); } 
//]]> 
</script> 

答えて

0

いかがhttp requestアプリからページ とページリターンjsonへ。もう一つのオプションは、JavaScriptから呼び出すことができますアプリ内で関数を作成することができるということです

+0

私はそれについて気付いています。しかし、私はhttp要求なしでこれを行うための新しい方法を模索したいと思います。 – Nik

+0

ファイヤーベースを使用する方法について – m0aaz

+0

提案していただきありがとうございますが、私が問題に言及した方法で解決策を欲しいです。 – Nik

1

(注意:これには注意してくださいまたはあなたはセキュリティ上の問題を作成することができます)、以下の

例はhttps://developer.android.com/guide/webapps/webview.html

から取られます

キーパーツ:

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 = (WebView) findViewById(R.id.webview); 
webView.addJavascriptInterface(new WebAppInterface(this), "Android"); 

HTMLページ:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> 

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

+0

驚くばかりです。しかし、私は現場での変更を望んでいません。ちょうど私がウェブサイトの現在のコードでそれを行うことができる方法を得たいと思います。 – Nik

0

にはJavaScriptブリッジは、あなたが探しているものであるのWebビューかもしれません!これはJSBridgeを使用するか、JSブリッジを独自に実装することができます。

関連する問題