解决方案很简单,很难找到/想,但它荒谬的简单,因为 html 标签中的运算符 == & 和其他元素,如 " 可能会使原因非常不可预测,所以我做了什么:
首先获取代码并使用 Apache Commons 转义:
例如:String newHtml = StringEscapeUtils.escapeHtml4(oldHtml) //oldHtml = String too ofc
现在您必须将其发送到 javascript,但您必须插入所有 btw 引号,但再次,您不能单独使用正常的引号,您需要将 \ 放在前面,所以:
PS:第一个报价可以正常,其他不行..
webview.loadUrl("javascript:function(" + JavaScriptArgsSTRING + ");
好吧,我知道我没疯,这只是为了说明你需要注意的地方,现在是重要的部分:
在你在 html 中进行转义后,它会像被窃听一样奇怪,因为会改变操作符(不要问我,我不知道这东西是如何工作的,但我知道它现在可以工作 =D)
例子,js函数的1个参数
String JavaScriptArgsSTRING = " \""+ newHtml + "\"";
\" will make quotes REAL in the String, like every String s = ""; need
explaining in order:
" before javascript => start the String args
" => stop the string and
start variable space
\" => going to make real quote in the string, thats what the js FUNCION need to see
" => you need to stop the String
start created by \"
+ => concatenation of the string/html whatever you want to send, ofc html code need Escape
newHtml => the var
+ => concatenation
" => stop the variable \" => close the function arg inside REAL "
(again, its what js function want to see)
" => stop the String start created by the second \"
就是这样,如果您想在函数中发送更多参数,只需复制并粘贴如下:
String JavaScriptArgsSTRING = " \""+ newHtml + "\" , \""+ newHtml + "\"";
继续,只需添加更多,然后粘贴...(注意,当您在 IDE 中粘贴时,它会自动添加更多“,因此请查找
我花了 2 天时间寻找避免语法错误的方法,这就是工作的方式,至少对我来说......
抱歉英语不好(有人会修复它=x)和一些愚蠢的解释(我不是真正的开发人员,但我喜欢使用它)