【问题标题】:Is it possible to pass data from app to a web form using webview?是否可以使用 webview 将数据从应用程序传递到 Web 表单?
【发布时间】:2017-08-08 01:06:03
【问题描述】:

我正在尝试通过我的移动应用程序使用 Javascript 注入将数据从应用程序传递到我的 web 视图的字段。我有一个网站表单,其中包含姓名、电子邮件等字段。姓名和电子邮件以及所有其他信息都在我的本地移动应用程序中。我想将该相关信息传递给相应的 Web 表单字段。这可能吗?以下是我尝试过的。

        wv = (WebView) mRootView.findViewById(R.id.wv_help);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                // do nothing
            }

            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // do nothing
            }

            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, @NonNull WebResourceRequest req, @NonNull WebResourceError err) {
                // do nothing
            }
        });

        String htmlValue = "<html xmlns=\"https://my_web_form.com\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><title>Lorem Ipsum</title></head><body style=\"width:300px; color: #00000; \"><p><strong> About us</strong> </p><p><strong> Lorem Ipsum</strong> is simply dummy text</p></body></html>";
        wv.loadDataWithBaseURL("https://my_web_form.com", htmlValue, "text/html; charset=utf-8", "UTF-8", "");

上述代码的结果是带有元数据的空白屏幕。我希望能够将元数据文本添加到我的网络表单上的特定字段。

【问题讨论】:

  • 如果您要投反对票,请解释您的理由。我提出的问题是合法的,并且遵循 SO 规则/标准。

标签: javascript android webview


【解决方案1】:

WebView 有postUrl 功能。

WebView webview = new WebView(this);
setContentView(webview);
String url = "http://www.example.com";
String postData = "Your Post data here";
webview.postUrl(url,postData.getBytes());

【讨论】:

  • 如果我有一个包含名字的表格,例如姓氏。 post URL 如何允许我通过将数据从我的移动应用程序传递到这些相关字段来填充该表单。了解这不是一个网络应用程序。它是一个使用 webview 的原生应用程序。
  • 赞这个String postData = "firstname=" + URLEncoder.encode(my_firstname, "UTF-8") + "&amp;lastname=" + URLEncoder.encode(lastname, "UTF-8");
【解决方案2】:

是的,可以使用 postUrl 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2013-12-15
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多