Passing data from java class to Web View html

前端 未结 5 2169
情书的邮戳
情书的邮戳 2020-11-27 03:52

I\'m loading below html in my webView

https://mail-attachment.googleusercontent.com/attachment/?ui=2&ik=25c0c425c6&view=att&th=138db54ff27a

5条回答
  •  渐次进展
    2020-11-27 04:38

    Be careful to call javascript function like this, the str may include single quote or other special characters.

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

    I suggest to encode the str in base64, and decode it on javascript side.

    • Android

      String str = "xxx";
      //encode in base64
      String base64Str = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
      myWebView.loadUrl("javascript:xxx('"+ base64Str +"')");
      
    • Javascript

      function xxx(val) {
          //decode from base64
          var str = atob(data)
      }
      

提交回复
热议问题