2

Xamarin フレームワークを使用して HTML5 アプリのプロトタイプを作成しています。JavaScript を C# コードに、またはその逆に接続しようとしています。

これを行う最善の方法は何ですか?これまでのところ、Cordova フレームワークへの Mono C# バインディングを作成しようとしましたが、これは理想的なソリューションではなく、一般的な .Net 中心のアプローチを利用するより簡単なものを探しています。そこで、javacript 呼び出しからのアプリの http 要求を処理するように HttpListener を設定しようとしました。この方法は Monotouch ではうまく機能しますが、Mono for Android では機能しません。

Javascript 呼び出し:

<script type="text/javascript">
    function myFunction()
    {
        var request = new XMLHttpRequest();
        request.open('GET','http://127.0.0.1:8001/callnative', false);
        request.send();

        if(request.status == 200){
            alert("Callback!");
        }
        else{
            alert("Error");
        }
    }
</script>

Activity クラスの C# コードは次のとおりです。

[Activity (Label = "AnroidHTML5Prototype", MainLauncher = true)]
    public class Activity1 : Activity
    {
        int count = 1;

        public HttpListener listener;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetupListener ();

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };

            LoadWebView ();
        }

        private void LoadWebView()
        {
            string lfn = "index.html";
            string html = string.Empty;

            // context could be ApplicationContext, Activity or
            // any other object of type Context
            using (var input = this.ApplicationContext.Assets.Open(lfn))
                using (StreamReader sr = new System.IO.StreamReader(input))
            {
                html = sr.ReadToEnd();
            }

            WebView webView = FindViewById<WebView> (Resource.Id.webView1);
            webView.SetWebChromeClient(new WebChromeClient());
            webView.Settings.JavaScriptEnabled = true;
            webView.LoadData (html, "text/html", null);
            webView.Settings.AllowFileAccess = true;
            webView.Settings.BlockNetworkLoads = false;
        }

        private void SetupListener() {
            listener = new HttpListener();
            listener.Prefixes.Add("http://*:8001/");
            listener.Start();

            listener.BeginGetContext(new AsyncCallback(HandleRequest), listener);
        }

        public void HandleRequest (IAsyncResult result)
        {
            RunOnUiThread(delegate{

                //Get the listener context
                HttpListenerContext context = listener.EndGetContext(result);

                //Start listening for the next request
                listener.BeginGetContext(new AsyncCallback(HandleRequest), listener);

                string response = String.Empty;

                if (context.Request.Url.ToString ().Contains ("callnative")) {
                    response = "native code reply";
                }

                byte[] responseBytes = System.Text.Encoding.UTF8.GetBytes(response);

                context.Response.ContentType = "text/json";
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                context.Response.ContentLength64 = responseBytes.Length;
                context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
                context.Response.OutputStream.Close();


//          WebView webView = FindViewById<WebView> (Resource.Id.webView1);
//          webView.LoadUrl("javascript:(function() { " +  
//                          "alert('Callback!'); " +  
//                          "})()"); 

            });
        }
    }

同様のコードは Monotouch では問題なく動作しますが、Mono for Android では次のエラーが発生します。null からのリクエストは作成できません:1 [Web コンソール] Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101:1

これは Chrome ブラウザのファイル アクセス許可に関係していると思われるため、WebView の AllowFileAccess 設定を true に設定しましたが、それでも同じエラーが発生します。

Mono で javascript と C# の間のブリッジを実現するより良い方法、または HTTPListener コードを Android で動作させる方法はありますか?

この記事は Xamarin のドキュメント検索で出てきますが、404 です。

http://docs.xamarin.com/recipes/android/controls/webview/call_c#_from_javascript

ありがとうデリック

4

3 に答える 3

1

I was able to connect JS and C# in Mono for Android by extending the WebViewClient and overriding the ShouldOverrideUrlLoading method:

Activity Class:

using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using System.IO;
using System.Net;
using System.Text;

namespace AnroidHTML5Prototype
{
    [Activity (Label = "AnroidHTML5Prototype", MainLauncher = true)]
    public class Activity1 : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            WebView webView = new WebView (this);
            webView.Settings.JavaScriptEnabled = true;
            webView.SetWebChromeClient(new WebChromeClient());
            string html = "<html><head><title></title></head><body><h1 id='button'>Hello Android</h1><h1 id='message' style='color:red'></h1><script>document.getElementById('button').onclick=function(){window.location='/callnative';}; function callback(msg){document.getElementById('message').innerHTML = msg;}</script></body></html>";
            webView.LoadData (html, "text/html", null);
            webView.SetWebViewClient (new MyWebViewClient(webView));

            base.OnCreate (bundle);
            SetContentView (webView);
        }
    }
}

Extended WebViewClient class:

using System;
using Android.Webkit;
using Java.IO;
using Java.Net;

namespace AnroidHTML5Prototype
{
    public class MyWebViewClient : WebViewClient
    { 
        WebView webView;
        public MyWebViewClient (WebView webView) : base()
        {
            this.webView = webView;
        }

        public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {
            if (url == "/callnative") 
            {
                webView.LoadUrl ("javascript:callback('Hello from Android Native');");
                return true;
            }

            return base.ShouldOverrideUrlLoading(view, url);
        }
    }
}
于 2013-06-05T18:23:44.853 に答える
0

興味のある方へ - 最初のコード サンプルを実行せずに実行できました

RunOnUiThread(delegate{

また、htmlファイルでjQuery AJAX呼び出しを使用しました

<script type="text/javascript" src="../../js/jquery-1.9.1.js"></script>
     ...
<script type="text/javascript">
    $(document).ready(function () {
        $('#example-1').click(function () {
            $.ajax({
                async: true,
                url: 'http://localhost:8801/test/url/test.htm'
            });
            $(this).load('ProgressDialog.htm');
        });
    });
</script>  

127.0.0.1の代わりにlocalhostが使用されているため、基本的に同じコードが Xamarin.iOS と Xamarin.Android の両方で機能することにも注意してください。

MonoWebKit.NETおよびMonobjcWebKit WebView コントロールとして使用して、 MacWindowsで同じコードベースを共有するクロスプラットフォームのデスクトップ HTML アプリを作成しました。ところで、InApps for Mac を含む完全に無料のツールセット (はい、Miguel、プラットフォームあたり 999 ドルは、私を含むほとんどの人にとっては高すぎます ;)そのアプリの作業から、次のようなものだけがわかっています

.EvaluateJavaScript(..)

メイン(別名UI)スレッド(そしてもちろんすべてのUIのもの)で呼び出す必要があるため、質問のコードサンプルでRunOnUiThreadを使用した理由がわかりません。

それが誰かを助けることを願っています:)

于 2014-03-18T03:35:21.197 に答える
0

Android では、localhost IP (エミュレーションが実行されているコンピューターの IP) は 127.0.0.1 ではありません

エラーの原因である場合とそうでない場合があるのは、10.0.2.2 refです。

于 2013-06-05T23:39:30.080 に答える