webview处理404错误[转载]

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;
        }
       
}

你可能感兴趣的:(webView)