如何连接 webviewclient 和 Chromeweb 视图客户端
Posted
技术标签:
【中文标题】如何连接 webviewclient 和 Chromeweb 视图客户端【英文标题】:How to connect webviewclient and Chromeweb view client 【发布时间】:2018-12-16 05:06:56 【问题描述】:我已经在我的 web 视图中创建了进度条。所以它会在加载 web 视图后消失。现在我想添加 error.html 页面以显示没有互联网消息..我有代码但我不知道如何连接他们...帮帮我
这是我的代码:
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
browserSetting.setjavascriptEnabled(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient()
public void onProgressChanged(WebView view, int progress)
progressBar.setProgress(progress);
textView.setText(progress + " %");
);
private class MyWebViewClient extends WebViewClient
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
@Override
public void onPageFinished(WebView view, String url)
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
这里是error.html代码
webView.setWebViewClient(new WebViewClient()
public void onRecievedError(WebView view ,int errorCode,String description ,String failingUrl)
webView.loadUrl("file:///android_asset/error.html");
);
之后
【问题讨论】:
很抱歉你的问题我不清楚 我想在上面的大代码中添加error.html代码 【参考方案1】:用此代码替换您的 MyWebViewClient :
private class MyWebViewClient extends WebViewClient
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
@Override
public void onPageFinished(WebView view, String url)
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
super.onReceivedError(view, request, error);
view.loadUrl("file:///android_asset/error.html");
【讨论】:
以上是关于如何连接 webviewclient 和 Chromeweb 视图客户端的主要内容,如果未能解决你的问题,请参考以下文章
如何从不在 Android 的 API 26 上的 WebView 获取 WebviewClient 实例?
如何从 webViewClient.shouldInterceptRequest() 返回 Http 错误响应?
WebViewClient将数据返回到Activity onPageFinished