我需要从网络响应中调用我的java函数:
这是我的代码,请指出我哪里出错了
活动:
@InjectView(R.id.loadCardWebView)
android.webkit.WebView webView;
WebViewClient webViewClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
ButterKnife.inject(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(false);
webView.addJavascriptInterface(new JSInterface(activity), "XYZ");
initWebViewClient();
webView.loadUrl("file:///android_asset/www/response.html");
}
private void initWebViewClient() {
webViewClient = new WebViewClient() {
private int webViewPreviousState;
private final int STARTED = 1;
private final int REDIRECTED = 4;
@Override
public boolean shouldOverrideUrlLoading(android.webkit.WebView view,
String urlNewString) {
webViewPreviousState = REDIRECTED;
webView.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = STARTED;
}
@Override
public void onPageFinished(android.webkit.WebView view, String url) {
if (webViewPreviousState == STARTED) {
Log.e(TAG ,RESPONSE URL : " + url);
}
}
};
webView.setWebViewClient(webViewClient);
}
MY JAVASCRIPT CLASS:
public class JSInterface {
private Activity activity;
public JSInterface(Activity activity) {
this.activity = activity;
}
@JavascriptInterface
public void showToast(String string){
Log.e("REACHED","HERE");
Timber.e("REACHED HERE");
}
}
这是一个示例HTML文件:
<html>
<body>
<a href="test_2.html" target="_blank">goto test 2</a>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
XYZ.showToast(toast);
}
</script>
</body>
</html>
show方法函数永远不会被调用。我从服务器得到一个响应,它包含类似的代码,用于调用我的javascript类中的一个函数。
请帮忙!
提前致谢!
答案 0 :(得分:-1)
我不确定Timber.e()
是什么,但您的代码是否真的试图显示吐司信息?也许它应该更像......
@JavascriptInterface
public void showToast(String string) {
Toast.makeText(...);
Log.e("REACHED","HERE");
Timber.e("REACHED HERE");
}