所有错误页面在 WebView 自定义
All Errorpages customize at WebView
我的 WebView 应用程序有一些问题。
对于所有可能的错误页面(例如连接丢失、服务器超时等),我想要一个在应用程序中离线可用的自定义错误页面。
这意味着如果出现任何问题,将重定向到 webview 中的离线页面。
我不想在 TimeOut 或 Internet 连接丢失之间做出区分,我只想将所有可能的网络错误重定向到离线站点。
这可能吗?
I just wanna one redirect for all possible web-errors to the offline site.
我认为如果发生任何错误,您需要自定义 WebView
class 用于 URL 重定向。
MainActivity.java
/* ... */
public class MainActivity extends AppCompatActivity {
MyWebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (MyWebView) findViewById(R.id.web_view);
mWebView.loadUrl("https://www.google.com/");
}
}
activity_main.xml
<!-- ... -->
<your_package_name.MyWebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- ... -->
MyWebView.java
/* ... */
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
initView(context);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
@SuppressLint("SetJavaScriptEnabled")
private void initView(Context context){
this.getSettings().setJavaScriptEnabled(true) ;
this.getSettings().setUseWideViewPort(true);
this.getSettings().setLoadWithOverviewMode(true);
this.getSettings().setDomStorageEnabled(true);
this.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(final WebView view, int errorCode, String description,
final String failingUrl) {
MyWebView.this.loadUrl("file:///android_asset/error.html");
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError errorResponse) {
MyWebView.this.loadUrl("file:///android_asset/error.html");
}
});
}
}
记得把你的error.html
放在app/src/main/assets
里面
我的 WebView 应用程序有一些问题。
对于所有可能的错误页面(例如连接丢失、服务器超时等),我想要一个在应用程序中离线可用的自定义错误页面。
这意味着如果出现任何问题,将重定向到 webview 中的离线页面。
我不想在 TimeOut 或 Internet 连接丢失之间做出区分,我只想将所有可能的网络错误重定向到离线站点。
这可能吗?
I just wanna one redirect for all possible web-errors to the offline site.
我认为如果发生任何错误,您需要自定义 WebView
class 用于 URL 重定向。
MainActivity.java
/* ... */
public class MainActivity extends AppCompatActivity {
MyWebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (MyWebView) findViewById(R.id.web_view);
mWebView.loadUrl("https://www.google.com/");
}
}
activity_main.xml
<!-- ... -->
<your_package_name.MyWebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- ... -->
MyWebView.java
/* ... */
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
initView(context);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
@SuppressLint("SetJavaScriptEnabled")
private void initView(Context context){
this.getSettings().setJavaScriptEnabled(true) ;
this.getSettings().setUseWideViewPort(true);
this.getSettings().setLoadWithOverviewMode(true);
this.getSettings().setDomStorageEnabled(true);
this.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(final WebView view, int errorCode, String description,
final String failingUrl) {
MyWebView.this.loadUrl("file:///android_asset/error.html");
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError errorResponse) {
MyWebView.this.loadUrl("file:///android_asset/error.html");
}
});
}
}
记得把你的error.html
放在app/src/main/assets