سؤال

I`m just trying to pass values from my app to the webview using the examples from android developer page, throught javascript.

My webview is set with:

WebView navegador;
final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);
navegador.getSettings().setJavaScriptEnabled(true);
navegador.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
navegador.addJavascriptInterface(myJavaScriptInterface, "navegador");
navegador.loadUrl(url);

Right under it i create the JavaScriptInterface class:

    public class JavaScriptInterface {
    Context mContext;

    JavaScriptInterface(Context c) {
        System.out.println("FaceWeb -> Navegador.java -> JavascriptInterface");
        mContext = c;
    }

    public String getLogin(){
        System.out.println("FaceWeb -> Navegador.java -> getLogin: "+b.getString("login"));
        return b.getString("login"); 
    }

    public String getNome(){
        System.out.println("FaceWeb -> Navegador.java -> getNome: "+ b.getString("nome"));
        return b.getString("nome"); 
    }

    public Integer getId(){
        System.out.println("FaceWeb -> Navegador.java -> getId: "+ b.getInt("id"));
        return b.getInt("id");
    }
}

And here my html:

<body>
<div id="teste">
Parado.
</div>

<input type="button" onclick="leitura()" value="Buscar Valores" />

<script type="text/javascript">
function leitura()
{
document.getElementById("teste").innerHTML = "Aguarde...";

var fonte = navegador.getLogin();
var id = navegador.getId();
var nome = navegador.getNome();
document.getElementById("teste").innerHTML = "Login: "+fonte + "<br>Nome: "+nome+"<br>ID: "+id;

}

</script>

</body>

In Eclipse log i can see that the functions getLogin, getId and getNome are called when the button is pressed on the webview, with right values, but my div "teste" remains with "Aguarde..." only and the button itself remains 'pressed'.

Any ideas please, reply. I really don`t know where to look anymore.

هل كانت مفيدة؟

المحلول

Try with public int getId() instead of Integer. Try with calling getLogin and getNome only. See if getId is the problemmaker. If you comment the last innerHtml out, does the button come up then?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top