博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webview处理404错误[转载]
阅读量:6119 次
发布时间:2019-06-21

本文共 2186 字,大约阅读时间需要 7 分钟。

hot3.png

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebView_404 extends Activity {
        private final String HOMEPAGE = "http://10.0.0.95/index.html";//请求的网站的主页
        private WebView web;
        private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        if(msg.what==404) {//主页不存在
                                //载入本地assets文件夹下面的错误提示页面404.html
                                web.loadUrl("file:///android_asset/404.html");
                        } else {
                                web.loadUrl(HOMEPAGE);
                        }
                }
        };
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.webview_404);
                web = (WebView) findViewById(R.id.WebView01);
                web.getSettings().setJavaScriptEnabled(true);
                web.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                if(url.startsWith("http://") && getRespStatus(url)==404) {
                                        view.stopLoading();
                                        //载入本地assets文件夹下面的错误提示页面404.html
                                        view.loadUrl("file:///android_asset/404.html");
                                } else {
                                        view.loadUrl(url);
                                }
                                return true;
                        }
                });
                new Thread(new Runnable() {
                        @Override
                        public void run() {
                                Message msg = new Message();
                                //此处判断主页是否存在,因为主页是通过loadUrl加载的,
                                //此时不会执行shouldOverrideUrlLoading进行页面是否存在的判断
                                //进入主页后,点主页里面的链接,链接到其他页面就一定会执行shouldOverrideUrlLoading方法了
                                if(getRespStatus(HOMEPAGE)==404) {
                                        msg.what = 404;
                                }
                                handler.sendMessage(msg);
                        }
                }).start();
        }
       
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_BACK && web.canGoBack()) {
                        web.goBack();
                        return false;
                }
                return super.onKeyDown(keyCode, event);
        }
       
        private int getRespStatus(String url) {
                int status = -1;
                try {
                        HttpHead head = new HttpHead(url);
                        HttpClient client = new DefaultHttpClient();
                        HttpResponse resp = client.execute(head);
                        status = resp.getStatusLine().getStatusCode();
                } catch (IOException e) {}
                return status;
        }
       
}

转载于:https://my.oschina.net/u/171154/blog/53553

你可能感兴趣的文章
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
前端学习之正则表达式
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
codeforce 599B Spongebob and Joke
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
9、Dubbo-配置(4)
查看>>
前端第七天
查看>>
BZOJ 2190[SDOI2008]仪仗队
查看>>
图解SSH原理及两种登录方法
查看>>
[转载] 七龙珠第一部——第058话 魔境圣地
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>