如何防止 URL 在网页视图中显示

huangapple go评论74阅读模式
英文:

How to prevent url to show in web view

问题

你好,我正在尝试创建一个应用程序,在应用中我使用了网页视图来展示我的谷歌驱动器文件夹。但是每当在加载网址的过程中遇到断网情况,会显示一个包含正在显示的网址信息的消息。

我应该如何防止用户看到这些显示中的网址呢?
在网络连接在开始时或在开始加载后断开时,是否有可能显示一些其他的消息呢?

英文:

Hi there I am trying to create app where I am using web view to show my google drive folder but whenever internet got disconnect in between loading the URL at that a msg comes with showing the URL information.
What can I do to prevent showing URL from users.
Is it possible to show some other msg whenever internet goes down on starting or when it goes down on after starting loading.

答案1

得分: 0

可以!您可以在应用程序启动后立即检查以下方法,然后打印消息或将用户重定向到其他活动...

DD4YouConfig dd4YouConfig = new DD4YouConfig(context);

if (dd4YouConfig.isInternetConnectivity()) {
    //重定向到 Webview
}
else {
    //调用警告对话框,指示无互联网连接
}

这是一个库函数,所以在 Gradle 中务必不要忘记添加以下行:

implementation 'in.dd4you.appsconfig:appsconfig:1.3.3'
英文:

Yes! You can either print the message or redirect the user to some other activity by checking the following method soon after the app gets launches...

    DD4YouConfig dd4YouConfig = new DD4YouConfig(context);

    if (dd4YouConfig.isInternetConnectivity()) {

    //redirect to webview
    }
   
    else 
    {
       //call alert dialog stating no internet
    }

This is a library function so obviously don't fail to add this line in Gradle

> implementation 'in.dd4you.appsconfig:appsconfig:1.3.3'

答案2

得分: 0

webView.setWebViewClient(new WebViewClient(){
	@Override
	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
		switch(errorCode){
			case ERROR_HOST_LOOKUP:
				webView.loadDataWithBaseURL(null, "<自定义的HTML页面,用于在出现错误时显示>", "text/html", "UTF-8", null);
				break;
			case ERROR_CONNECT:
				webView.loadDataWithBaseURL(null, "<自定义的HTML页面,用于在出现错误时显示>", "text/html", "UTF-8", null);
				break;
			case ...[如果需要捕获更多错误]
		}
	}
});

参考链接:https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedError(android.webkit.WebView,%20int,%20java.lang.String,%20java.lang.String)

错误代码参考:
https://developer.android.com/reference/android/webkit/WebViewClient#ERROR_AUTHENTICATION

英文:

I have a custom HTML page that will show if something wrong happened when loading the url.

webView.setWebViewClient(new WebViewClient(){
	@Override
	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
		switch(errorCode){
			case ERROR_HOST_LOOKUP:
				webView.loadDataWithBaseURL(null,&quot;&lt;YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE&#39;S AN ERROR&gt;&quot;, &quot;text/html&quot;, &quot;UTF-8&quot;,null);
				break;
			case ERROR_CONNECT:
				webView.loadDataWithBaseURL(null,&quot;&lt;YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE&#39;S AN ERROR&gt;&quot;, &quot;text/html&quot;, &quot;UTF-8&quot;,null);
				break;
			case ...[IF YOU WANT TO CATCH MORE ERRORS]
		}
	}
}

Reference: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedError(android.webkit.WebView,%20int,%20java.lang.String,%20java.lang.String)

Error Code Reference:
https://developer.android.com/reference/android/webkit/WebViewClient#ERROR_AUTHENTICATION

huangapple
  • 本文由 发表于 2020年9月30日 02:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定