Android 读取网络视图 html 并将数据发送到 java

Android read webview html and send data to java

我在网络视图中加载了一个 HTML 表单。提交此表格后,会打开一个感谢页面。其中有以下 html 内容。我不控制 web/html 代码的任何部分,因为它完全是在像 landinggi

这样的第三方服务中制作的
<head>
   <script>
           var conversion = { hash: '3bd15c781a094e1fd0f079327c659f95','title': "Test - New Zealand",'name': "12\/12\/2019",'text': "10",'text1': "2",'phone1': "995309934",'textarea': "Test Request" };
   </script>

我想在这个JS模型中提取数据,最好存储在一个Java形式的模型中。

Class Conversion{
      public String hash;
      public String title;
      public String name;
      public String text; ...
}

有没有一种干净的方法来做这样的事情?

创建了以下方法,从 webview 读取数据并填充我的 Java 对象。 注意:- 所有评估调用都是异步的,因此仍需要了解如何处理此信息。

private void fetchWebViewDataForConversion(){
    ConversionModel model = new ConversionModel();
    webview.evaluateJavascript(
            "(function() { return conversion.hash; })();", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                    model.hash = s;
                    Log.d(Config.LOGTAG, s); // Returns the value from the function
                }
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.title; })();", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                    model.destination = s;
                    Log.d(Config.LOGTAG, s); // Returns the value from the function
                }
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.name; })();", s -> {
                model.date = s;
                Log.d(Config.LOGTAG, s); // Returns the value from the function
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.text; })();", s -> {
                model.numberOfNights = s;
                Log.d(Config.LOGTAG, s); // Returns the value from the function
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.text1; })();", s -> {
                model.numberOFPassengers = s;
                Log.d(Config.LOGTAG, s); // Returns the value from the function
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.phone1; })();", s -> {
                model.phoneNumber = s;
                Log.d(Config.LOGTAG, s); // Returns the value from the function
            }
    );
    webview.evaluateJavascript(
            "(function() { return conversion.textarea; })();", s -> {
                model.details = s;
                Log.d(Config.LOGTAG, s); // Returns the value from the function
            }
    );
}