每天記錄學習的新知識 :WebViewClient#shouldOverrideUrlLoading 超鏈接攔截

簡介:

超鏈接:指從一個網頁指向一個目標的連接關係,當瀏覽者單擊已經鏈接的文字或圖片後,鏈接目標將顯示在瀏覽器上,並且根據目標的類型來打開或運行。在本質上屬於一個網頁的一部分,它是一種允許我們同其他網頁或站點之間進行連接的元素。

使用介紹:

2.1 重寫的方法:

想要攔截超鏈接,需要重寫一下如下方法:

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

當點擊的是超鏈接的時候,shouldOverrideUrlLoading()會回調。

提示:在Android N中 shouldOverrideUrlLoading(WebView view, String url)被廢棄,shouldOverrideUrlLoading()是替代方法。

2.2 方法內部數據:

2.2.1 WebResourceRequest 是我們的數據類型。

abstract String	getMethod()
Gets the method associated with the request, for example "GET". 獲取請求方法

abstract Map<String, String>	getRequestHeaders() 
Gets the headers associated with the request. 獲取請求頭結構

abstract Uri	getUrl() 
Gets the URL for which the resource request was made. 獲取URI
abstract boolean	hasGesture()
Gets whether a gesture (such as a click) was associated with the request.

abstract boolean	isForMainFrame()
Gets whether the request was made in order to fetch the main frame's document.

abstract boolean	isRedirect()
Gets whether the request was a result of a server-side redirect.

2.2.2 解析一下URI

我們需要知道想攔截的超鏈接是哪個,就需要解析一下URI的內容。

萬能打印Log:

        LogUtils.i(TAG, "shouldOverrideUrlLoading ==" + new Gson().toJson(request));
        LogUtils.i(TAG, "shouldOverrideUrlLoading ==" + new Gson().toJson(request.getRequestHeaders()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading ==" + new Gson().toJson(request.getMethod()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading ==" + new Gson().toJson(request.getUrl()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedPath ==" + new Gson().toJson(request.getUrl().getEncodedPath()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getAuthority ==" + new Gson().toJson(request.getUrl().getAuthority()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getHost ==" + new Gson().toJson(request.getUrl().getHost()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedQuery ==" + new Gson().toJson(request.getUrl().getEncodedQuery()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getScheme ==" + new Gson().toJson(request.getUrl().getScheme()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedAuthority ==" + new Gson().toJson(request.getUrl().getEncodedAuthority()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedFragment ==" + new Gson().toJson(request.getUrl().getEncodedFragment()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedSchemeSpecificPart ==" + new Gson().toJson(request.getUrl().getEncodedSchemeSpecificPart()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getEncodedUserInfo ==" + new Gson().toJson(request.getUrl().getEncodedUserInfo()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getFragment ==" + new Gson().toJson(request.getUrl().getFragment()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getPath ==" + new Gson().toJson(request.getUrl().getPath()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getUserInfo ==" + new Gson().toJson(request.getUrl().getUserInfo()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getSchemeSpecificPart ==" + new Gson().toJson(request.getUrl().getSchemeSpecificPart()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getQueryParameterNames ==" + new Gson().toJson(request.getUrl().getQueryParameterNames()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getQuery ==" + new Gson().toJson(request.getUrl().getQuery()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getPort ==" + new Gson().toJson(request.getUrl().getPort()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getPathSegments ==" + new Gson().toJson(request.getUrl().getPathSegments()));
        LogUtils.i(TAG, "shouldOverrideUrlLoading getLastPathSegment ==" + new Gson().toJson(request.getUrl().getLastPathSegment()));
  1. Scheme
    他的值可以是:tel 、http ,目前用到這麼兩種,或許還會有htttps,可以用來判斷動作

  2. host
    當scheme 是tel,host 的值將會是電話號碼;
    當scheme是http,host的值將會是網址鏈接。

  3. 其他的值,我沒有用上,如果有需要打log看吧。

2.2.3 url 轉碼

當Client向服務器請求,需要進行url轉碼:

url = "xxxxx?param=" + URLEncoder.encode("xxx", "utf-8");

服務器收到數據後,進行解碼:

String param = URLDecoder.decode(param, "utf-8");

因爲,我們在2.2.2中獲取的host就是服務器參數,所以,我們在使用host的時候,需要進行解碼:

        String hostDecode;
        try {
            hostDecode = URLDecoder.decode(host, "utf-8");
        } catch (UnsupportedEncodingException e) {
            hostDecode = "";
            e.printStackTrace();
        }

解碼出來的數據,我們纔可以使用,不然不好用。

爲什麼需要轉碼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章