Javascript tải tệp html cục bộ

Bạn có thể cung cấp nội dung dựa trên web—chẳng hạn như HTML, JavaScript và CSS—để ứng dụng của bạn sử dụng mà bạn biên dịch tĩnh vào ứng dụng thay vì tìm nạp qua internet

Nội dung trong ứng dụng không yêu cầu truy cập internet hoặc tiêu tốn băng thông của người dùng và nếu nội dung được thiết kế dành riêng cho chỉ dành cho WebView—nghĩa là nội dung đó phụ thuộc vào việc giao tiếp với ứng dụng gốc—thì người dùng không thể vô tình tải nội dung đó vào một

Tuy nhiên, có một số nhược điểm đối với nội dung trong ứng dụng. Cập nhật nội dung dựa trên web yêu cầu gửi bản cập nhật ứng dụng mới và có khả năng nội dung không khớp giữa nội dung trên trang web và nội dung trong ứng dụng trên thiết bị của bạn nếu người dùng có phiên bản ứng dụng lỗi thời

WebViewAssetLoader

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
4 là một cách linh hoạt và hiệu quả để tải nội dung trong ứng dụng trong một đối tượng
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
5. Lớp này hỗ trợ

  • Đang tải nội dung bằng URL HTTP[S] để tương thích với chính sách cùng nguồn gốc
  • Đang tải các nguồn phụ như JavaScript, CSS, hình ảnh và iframe

Bao gồm

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
4 trong tệp hoạt động chính của bạn. Sau đây là một ví dụ về tải nội dung web đơn giản từ thư mục nội dung

Kotlin

private class LocalContentWebViewClient[private val assetLoader: WebViewAssetLoader] : WebViewClientCompat[] {
    @RequiresApi[21]
    override fun shouldInterceptRequest[
        view: WebView,
        request: WebResourceRequest
    ]: WebResourceResponse? {
        return assetLoader.shouldInterceptRequest[request.url]
    }

    // to support API < 21
    override fun shouldInterceptRequest[
        view: WebView,
        url: String
    ]: WebResourceResponse? {
        return assetLoader.shouldInterceptRequest[Uri.parse[url]]
    }
}

Java

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}

Ứng dụng của bạn nên định cấu hình phiên bản

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
4 để phù hợp với nhu cầu của nó. Phần tiếp theo có một ví dụ

Tạo nội dung và tài nguyên trong ứng dụng

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
4 dựa vào phiên bản
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
9 để tải tài nguyên tương ứng với đường dẫn tài nguyên nhất định. Mặc dù bạn có thể triển khai giao diện này để truy xuất tài nguyên khi ứng dụng của mình cần, nhưng thư viện Webkit gói

  
    
    
  
  
    

This file was loaded from in-app content

0 và

  
    
    
  
  
    

This file was loaded from in-app content

1 để tải nội dung và tài nguyên Android tương ứng

Để bắt đầu, hãy tạo một số nội dung và tài nguyên cho ứng dụng của bạn. Nói chung

  • Các tệp văn bản như HTML, JavaScript và CSS thuộc về nội dung
  • Hình ảnh và các tệp nhị phân khác thuộc về tài nguyên

Để thêm các tệp web dựa trên văn bản vào một dự án, hãy làm như sau

  1. Trong Android Studio, nhấp chuột phải vào ứng dụng > src > thư mục chính rồi chọn Mới > Thư mục


    Hình 1. Tạo một thư mục nội dung cho dự án của bạn

  2. Đặt tên cho thư mục "tài sản. "


    Hình 2. Đặt tên cho thư mục tài sản

  3. Bấm chuột phải vào thư mục nội dung rồi bấm Mới > Tệp. Nhập

    
      
        
        
      
      
        

    This file was loaded from in-app content

    2 và nhấn Return hoặc Enter


    Hình 3. Tạo các tệp có tên của các mẫu mã sau

  4. Lặp lại bước trước để tạo tệp trống cho

    
      
        
        
      
      
        

    This file was loaded from in-app content

    3

  5. Điền vào các tệp trống bạn đã tạo với nội dung trong hai mẫu mã tiếp theo

mục lục. html

________số 8

biểu định kiểu. css

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
3

Để thêm tệp web dựa trên hình ảnh vào dự án của bạn, hãy làm như sau

  1. Tải xuống tệp

    
      
        
        
      
      
        

    This file was loaded from in-app content

    4 về máy cục bộ của bạn

  2. Đổi tên tệp thành

    
      
        
        
      
      
        

    This file was loaded from in-app content

    5

  3. Di chuyển thủ công tệp vào thư mục

    
      
        
        
      
      
        

    This file was loaded from in-app content

    6 của dự án trên ổ cứng của bạn

Hình 4 hiển thị hình ảnh bạn đã thêm và văn bản từ các mẫu mã trước đó được hiển thị trong một ứng dụng


hinh 4. Tệp HTML trong ứng dụng và tệp hình ảnh được hiển thị trong ứng dụng

Để hoàn thành ứng dụng, hãy làm như sau

  1. Đăng ký trình xử lý và định cấu hình

    
      
        
        
      
      
        

    This file was loaded from in-app content

    7 bằng cách thêm đoạn mã sau vào phương thức
    
      
        
        
      
      
        

    This file was loaded from in-app content

    8

    Kotlin

    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    9

    Java

    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    0

  2. Tải nội dung bằng cách thêm đoạn mã sau vào phương thức

    
      
        
        
      
      
        

    This file was loaded from in-app content

    8

    Kotlin

    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    2

    Java

    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    3

Kết hợp nội dung trong ứng dụng với các tài nguyên từ trang web của bạn

Ứng dụng của bạn có thể cần tải hỗn hợp nội dung trong ứng dụng và nội dung từ internet, chẳng hạn như trang HTML trong ứng dụng được tạo kiểu bởi CSS của trang web của bạn.

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
4 hỗ trợ trường hợp sử dụng này. Nếu không ai trong số
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
31 đã đăng ký có thể tìm thấy tài nguyên cho đường dẫn đã cho, thì WebView sẽ quay lại tải nội dung từ internet. Nếu bạn đang trộn nội dung trong ứng dụng với các tài nguyên từ trang web của mình, hãy đảm bảo dành riêng các đường dẫn thư mục, chẳng hạn như
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
32 hoặc
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
33, cho các tài nguyên trong ứng dụng. Tránh lưu trữ bất kỳ tài nguyên nào từ trang web của bạn ở những vị trí đó

Kotlin

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
8

Java

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
9

Xem bản trình diễn WebView trên GitHub để biết ví dụ về trang HTML trong ứng dụng đang tìm nạp dữ liệu JSON được lưu trữ trên web

tảiDataWithBaseURL

Khi ứng dụng của bạn chỉ cần tải một trang HTML và không cần chặn các nguồn phụ, hãy cân nhắc sử dụng

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
34, không yêu cầu nội dung ứng dụng. Bạn có thể sử dụng nó như trong mẫu mã sau

Kotlin

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
0

Java

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
1

Chọn giá trị đối số một cách cẩn thận

  • private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    35. Đây là URL mà nội dung HTML của bạn sẽ được tải dưới dạng. Đây phải là một URL HTTP[S]
  • private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    36. Đây là nội dung HTML bạn muốn hiển thị, dưới dạng chuỗi
  • private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    37. Điều này thường nên được đặt thành
    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    38
  • private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    39. Điều này không được sử dụng khi
    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    35 là URL HTTP[S], vì vậy có thể được đặt thành
    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    91
  • private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    92. Giá trị này được đặt thành cùng giá trị với
    private static class LocalContentWebViewClient extends WebViewClientCompat {
    
        private final WebViewAssetLoader mAssetLoader;
    
        LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
            mAssetLoader = assetLoader;
        }
    
        @Override
        @RequiresApi[21]
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         WebResourceRequest request] {
            return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
        }
    
        @Override
        @SuppressWarnings["deprecation"] // to support API < 21
        public WebResourceResponse shouldInterceptRequest[WebView view,
                                         String url] {
            return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
        }
    }
    
    35

Chúng tôi thực sự khuyên bạn nên sử dụng URL HTTP[S] làm

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
35, vì điều này đảm bảo ứng dụng của bạn tuân thủ chính sách cùng nguồn gốc

Nếu bạn không thể tìm thấy một

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
35 phù hợp cho nội dung của mình và muốn sử dụng
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
96, thì bạn phải mã hóa nội dung bằng mã hóa phần trăm hoặc mã hóa Base64. Chúng tôi thực sự khuyên bạn nên chọn mã hóa Base64 và sử dụng API Android để mã hóa mã này theo chương trình, như được hiển thị trong mẫu mã sau

Kotlin

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
2

Java

private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
3

Thận trọng. Theo mặc định,
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
96 hy vọng dữ liệu HTML sẽ được mã hóa theo phần trăm. Mã hóa phần trăm bằng tay dễ bị lỗi và không có API Android nào để thực hiện việc này theo chương trình. Chúng tôi thực sự khuyên bạn nên chuyển sang
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient[WebViewAssetLoader assetLoader] {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi[21]
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     WebResourceRequest request] {
        return mAssetLoader.shouldInterceptRequest[request.getUrl[]];
    }

    @Override
    @SuppressWarnings["deprecation"] // to support API < 21
    public WebResourceResponse shouldInterceptRequest[WebView view,
                                     String url] {
        return mAssetLoader.shouldInterceptRequest[Uri.parse[url]];
    }
}
34 để tránh yêu cầu này hoặc sử dụng API Base64 để mã hóa nội dung, như được hiển thị trong mẫu mã trước đó

Làm cách nào để gọi tệp HTML cục bộ trong JavaScript?

The relevant code is: function ReadFile[filename] { try { var fso = new ActiveXObject["Scripting. FileSystemObject"]; var fh = fso.

Làm cách nào để đọc tệp cục bộ trong JavaScript?

Có thể mở và đọc các tệp cục bộ trong trình duyệt bằng cách sử dụng đối tượng Javascript FileReader. .
Bước 1 - Cho phép người dùng chọn tệp. .
Bước 2 — Đọc Siêu dữ liệu Tệp [Tên, Loại & Kích thước] bằng Thuộc tính của Đối tượng Tệp

Làm cách nào để tải HTML động bằng JavaScript?

Để tải động tệp JavaScript. Tạo phần tử tập lệnh. .
src. đường dẫn tập tin
loại. loại tệp - "văn bản/javascript"
không đồng bộ. nếu chúng ta đặt async thành false thì tệp sẽ được tải và thực thi trước khi chuyển sang hành động tiếp theo

Làm cách nào để tải một tệp trong JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the tags in your HTML document.

Chủ Đề