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;         }         }
相關文章
相關標籤/搜索