ichthyocentaurs
ichthyocentaurs

Reputation: 2175

Android read webview html and send data to java

I have an HTML form that I load up in a webview. On Submission this form opens up a Thank you page. Which has the following html content. I donot control any part of the web/html code since it is made entirely in a 3rd party service like 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>

I want to extract data in this JS model and preferably store it in a Java model of the form.

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

Is there a clean way to do something like this?

Upvotes: 0

Views: 348

Answers (1)

ichthyocentaurs
ichthyocentaurs

Reputation: 2175

Created the following method, to read data from a webview and fill my Java object. Note:- all evaluate calls are Async, so still need to understand how to process this information.

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
            }
    );
}

Upvotes: 1

Related Questions