Android: WebView加载网页的几种方式及网络异常处理
2016-04-12 14:22
204 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
1.加载本地assert目录下文件(error.html)
webcontent.loadUrl(" file:///android_asset/error.html ");
2.加载网络url(http://www.csdn.com)
webcontent.loadUrl(" http://www.csdn.com ");
3.加载 String 类型html
StringerrorHtml = "<html><body><h1>Page not find!</h1></body></html>";
webcontent.loadData(errorHtml, "text/html", "UTF-8");
4.加载SD卡html:
webcontent.loadUrl(" content://com.android.htmlfileprovider/sdcard/kris.html ");
异常处理:
webcontent.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.i("webview", "url = "+url);
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//这里进行无网络或错误处理,具体可以根据errorCode的值进行判断,做跟详细的处理。
view.loadUrl(file:///android_asset/error.html );
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url);
super.onPageFinished(view, url);
}
});
1.加载本地assert目录下文件(error.html)
webcontent.loadUrl(" file:///android_asset/error.html ");
2.加载网络url(http://www.csdn.com)
webcontent.loadUrl(" http://www.csdn.com ");
3.加载 String 类型html
StringerrorHtml = "<html><body><h1>Page not find!</h1></body></html>";
webcontent.loadData(errorHtml, "text/html", "UTF-8");
4.加载SD卡html:
webcontent.loadUrl(" content://com.android.htmlfileprovider/sdcard/kris.html ");
异常处理:
webcontent.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.i("webview", "url = "+url);
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//这里进行无网络或错误处理,具体可以根据errorCode的值进行判断,做跟详细的处理。
view.loadUrl(file:///android_asset/error.html );
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url);
super.onPageFinished(view, url);
}
});
相关文章推荐
- http 请求头
- 使用域账号统一管理cisco网络设备
- DL:卷积神经网络(CNN)的一些学习网址
- angularJS里的$http.post传数据到php接收问题
- golang http服务器跨域问题解决
- 神经网络第一部分:背景Neural Networks, Part 1: Background
- 神经网络第二部分:神经元Neural Networks, Part 2: The Neuron
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
- 【ZOJ3933 The 16th Zhejiang University Programming ContestG】【费用流】Team Formation 最多组队条件下女
- PHP CURL HTTP研究
- 从贝叶斯方法谈到贝叶斯网络
- java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
- 神经网络指南Hacker's guide to Neural Networks
- 学Http协议
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
- 卷积神经网络Convolutional Neural Networks
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
- http://与https://的区别