I got a string replace issue when I pass Java String into android webView through JavaScriptInterface.
Below is the HTML (utf-8 file) inside the webView:
<html>
<head>
<script type="text/javascript">
a=window.MyAndroid.getPicEncStr(); //get string from Java side.
function getValue(){
b="ue";
if(a==b) {
d="match";
} else {
d="not match";
}
c=d+":"+a.replace("u","0")+b.replace("u","0");
document.getElementById("test").innerHTML=c;
}
</script>
</head>
<body>
<span id="test">test</span>
</body>
</html>
JavaScriptInterface code are below:
import java.io.UnsupportedEncodingException;
import android.content.Context;
public class JavaScriptInterface {
private String unicodeToUtf8(String s) {
String str=null;
try {
str=new String( s.getBytes("utf-8") , "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public String getPicEncStr() {
//return "ue"; //not work
return this.unicodeToUtf8("ue"); //not work
}
}
The result in span("id"="test") is "match:ue0e" when I call webView.loadUrl("javascript:getValue()") no matter I convert from "unicode" to "utf-8" or not.
Basically string "a" equals to string "b", but the replace function doesn't work on a, only works on b.
Could someone help me with that?
Thanks.