Android如何拦截WebView之中的Post或者Get请求

  1. WebView webView = (WebView) findViewById(R.id.web_view);
  2. webView.setWebViewClient(new WebViewClient() {
  3.     @Override
  4.     public boolean shouldOverrideUrlLoading(WebView view, String url) {
  5.         Toast.makeText(getApplicationContext(),
  6.                  "WebViewClient.shouldOverrideUrlLoading",
  7.                  Toast.LENGTH_SHORT);
  8.         view.loadUrl(url);
  9.         return true;
  10.     }
  11.     
  12.     @Override
  13.     public void onPageStarted(WebView view, String url, Bitmap favicon) {
  14.         Toast.makeText(getApplicationContext(), 
  15.                       "WebViewClient.onPageStarted", 
  16.                       Toast.LENGTH_SHORT).show();
  17.         //这儿可以截获网页的URL,可以都URL进行分析。
  18.         //本例子之中是分析从通过RenRen登录成功后返回的access_token.
  19.         if (url.contains("graph.renren.com/oauth/login_success.html")) {
  20.             int start = url.indexOf("access_token") + "access_token=".length();
  21.             int end = url.indexOf("expires_in") - 1;
  22.             accessToken = url.substring(start, end);
  23.         }
  24.         super.onPageStarted(view, url, favicon);
  25.     }
  26. });
  27. webView.loadUrl(myUrl);

猜你喜欢

转载自wmcxy.iteye.com/blog/1847207