Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Try make the webView.loadUrl in utf8. stackoverflow.com/questions/13892210/… –  Andrew Nov 29 '13 at 3:16
    
Hi, Andrew, I tried, however, it still the same result. –  Box Very Dec 1 '13 at 22:10
add comment

1 Answer

It seems I find a way to solve the problem:

<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";
            }
            a=new String(a); //<-- force convert java string to javascript string.
            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>

Still don't understand why. When I dump "a", it shows "ue", when I try to dump "a[0]", it shows "undefined", however "b[0]" is "u", I only can image Java/Javascript string are not compatible with each other, so, a explicit conversion is necessary.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.