我没有明确的重现步骤,但我的javascript函数有时会返回' undefined'我无法解决它。
这是我第一次从android调用javascript,以填充html中的一些列表:
webView.addJavascriptInterface(new MyJavascriptInterface(), "android");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
final String paramsurl = "javascript:setProducts(\'" + demoStr + "\', \'" + visitedStr + "\');";
if (webView != null)
webView.loadUrl(paramsurl);
} else{
redirect = false;
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingFinished = false;
super.onPageStarted(view, url, favicon);
}
});
webView.loadUrl(url);
稍后,当用户点击Android UI中的发送按钮时,我希望从javascript接收结果
public void onSave(View v) {
PIWebView webView = (PIWebView) findViewById(R.id.webView1);
webView.loadUrl("javascript:android.getJsResult(sendFormEvent());");
}
最后,这是界面:
private class MyJavascriptInterface {
@JavascriptInterface
public void getJsResult(String msg) {
if (msg.equals("undefined")) {
msg = null;
}
mSampleProductJson = msg;
}
到目前为止一些想法,