android之webview使用-处理404等错误

jopen 11年前

    public class WebView_404 extends Activity {             private final String HOMEPAGE = "http://www.abchina.com";//请求的网站的主页             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.main);                     web = (WebView) findViewById(R.id.webview);                     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;             }         }