我正在使用webView在我的Android应用程序中播放Vimeo视频。但是,当发生错误时,webView会显示视频的URL,这对任何生产应用程序都是危险的。如何解决这个问题。请帮忙。下面是我当前的代码。
String vimeoVideo = "<html><body><iframe width="420" height="315" src="https://player.vimeo.com/video/163996646?player_id=player" frameborder="0" allowfullscreen></iframe></body></html>";
webView.setWebViewClient(new WebViewClient() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadData(vimeoVideo, "text/html", "utf-8");
只需在要捕获的代码周围创建一个 try/catch 块,当它导致异常并隐藏 WebView 元素时。
喜欢这个:
String vimeoVideo = "<html><body><iframe width="420" height="315" src="https://player.vimeo.com/video/163996646?player_id=player" frameborder="0" allowfullscreen></iframe></body></html>";
webView.setWebViewClient(new WebViewClient() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
try {
// Block of code to try
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadData(vimeoVideo, "text/html", "utf-8");
}
catch(Exception e) {
// Block of code to handle errors
webView.setVisibility(View.GONE);
System.out.println("Something went wrong.");
}