且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Java类将数据传递到Web查看HTML

更新时间:2023-12-03 16:05:04

首先,你的URL看起来不具备的。

First, your URL seems not available.

如果你想要做的Andr​​oid应用程序和Web应用程序/网页之间的数据交换,你可以通过javascript实现这一点。

If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.

下面是Android的官方网站的例子:

Here is an example from Android official site:

创建一个类是这样的:

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(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 = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

在您的网页:

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

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

如果你想传递的东西到您的网页,只需调用相应的JavaScript函数:

If you wanna pass something to your webpage, just calling corresponding javascript function:

String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");

下面是参考: http://developer.android.com/guide/webapps/webview.html