What is the method for importing a saved HTML into a WebView?

My WebView is supposed to load manipulated HTML saved in /klw.html. I am using the code

mWebView.loadUrl("file:///android_asset/klw.html");
. However, when testing it on the emulator, I keep getting a "Webpage not available" error. What could be causing this issue? Could it be that I need to load it from a different directory?

Below is the code snippet:

public class Stundenplan extends Activity {

    String url = "http://info.tam.ch/display/timetable_external.php?school=klw&sem=54&class=26&week=16";
    Document document;

    public void main(String... args){
        try {
            document = Jsoup.connect(url).get();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        Element head = document.head();
        head.append("<link rel=\"stylesheet\" href=\"klw.css\">");
        String html = document.html();

        String charset = Jsoup.connect(url).response().charset();
        
        try {
            Writer writer = new PrintWriter("/klw.html", charset);
            writer.write(html);
            writer.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        } 
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        main();
        WebView mWebView;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.loadUrl("file:///android_asset/klw.html");
    }
}

Answer №1

Have you included the necessary permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to ensure that the file can be written to?

Also, have you added the permission

<uses-permission android:name="android.permission.INTERNET">
to prevent the URI from being retrieved as NULL and then saved as a blank HTML?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The inability to convert a string into a JSON array is causing an issue

Is there a way to convert the data returned from the server that looks like this? [{"Food":"orange","Protein":"52","Water":"52","Magnesium":"12","Phosphorus":"12","Sodium":"12.5","Zinc":"45","Vit_C":"12","Fat_Sat":"12","Fat_Mono":"45","Vit_B12":"45","Calc ...

Encountered a STRING instead of an expected BEGIN_OBJECT at the beginning of the JSON file

Having trouble with an error message: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ Any ideas on how to fix this? This is the API end point: @FormUrlEncoded @Headers({"Accept: application/json"}) @POST("/api/regis ...

How to reduce the text input field size in Django Bootstrap for a sleeker design

I have a form with various elements like textfields, radio buttons, and text areas. The textfield size is currently too small and does not look good: https://i.sstatic.net/ghimd.png I'm wondering how I can make the textfields slightly larger and why ...

What is the process for incorporating Maven dependencies like Volley and Gson into a Bazel configuration for an Android development endeavor?

Currently, I am implementing the following code: deps = [ ":tensorflow_native_libs", "//tensorflow/contrib/lite/java:tensorflowlite", "@androidsdk//com.android.support:appcompat-v7-25.0.0", "@androidsdk//com.android.volley:volley:1.1.0", "@ ...

Exploring the Canvas with Full Element Panning, Minimap Included

Currently, I am working on incorporating a mini map onto my canvas that mirrors what is displayed on the main canvas. The main canvas includes zoom and pan functions. I have created a rectangular shape for the minimap to display the content of the canvas. ...

Storing information when an object is indexed in a for loop, to be retrieved later when

My JavaScript code includes an AJAX call that takes user input from a form and uses it to search for a JSON object. The matching objects are then displayed in a datalist successfully. Everything is working well so far, but now I want to extract specific f ...

What is the best way to set the tab as "active" in the first iteration of a PHP while loop?

When using a while loop to populate tabs with PHP, maintaining only one active tab at a time can be tricky. I have grasped that assigning the 'active' class within the loop causes all tabs to become active. So, how should I modify the script if I ...

Transferring data from an array to a view in an Android application

I have successfully retrieved data from a MySQL database and stored it in arrays in my android app using the code below: Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","lo ...

Hovering over an element will change its background color along with adjusting the background

I encountered a situation with the following code: CSS: <style> div.testy { border:1px solid black; } div.testy:hover { background-color:red; } </style> HTML: <div class="testy" style="height:100px; back ...

CSS - Maximizing One Column's Width While Allocating Percentage Space for Two Columns?

It's been quite some time since I delved into the world of web development and I seem to have forgotten how to tackle this particular issue. Despite hours spent searching online, I've gained knowledge about flex-boxes and other useful tools, but ...

Disregard the CSS styling within the modal window

Currently, I am working with ReactJS and ANTD. My modal has been adjusted with paddings to center-align the text, but now I want to enhance the design. Here is the desired look: https://i.stack.imgur.com/qef7n.png This is how it appears at the moment: htt ...

"Every time the $_POST array is accessed, it consistently contains 48 to

In my PHP file, I am querying a database to retrieve a list of items and then generating an HTML table to display those items. Each table cell contains input elements within a form <form> <table> ... <td> & ...

Issues with CodeIgniter Form Validation Implementation

Having trouble with form validation in CodeIgniter, not sure what's causing the issue! Below is my controller class: class registerController extends MY_Controller { // --- Methods ----------------- function __construct() { par ...

Changing the loading spinner icon in WooCommerce

Could someone help me change the loading spinner icon in WooCommerce? The current icon is defined in the woocommerce.css: .woocommerce .blockUI.blockOverlay::before { height: 1em; width: 1em; display: block; position: absolute; top: 50 ...

What is the reason that the 'mouseenter' event only applies to the initial element in each round of iteration within a spacebar loop?

My goal is to create an off-canvas menu within a template component. I found inspiration from this helpful article. The setup I have is quite common: A container tab where I loop through an items collection An item component that contains the off-canvas ...

The problem of a child div extending beyond the boundaries of its parent div

I am searching for a layout similar to this, click here. Here is the basic HTML code I have: #mainContent { margin:0; width:100%; height:600px; padding: 0 0 30px 0; } #mainContent #sidebar { /* sidebar inside # ...

A guide to personalizing the woocommerce 'best-selling items' widget

WooCommerce widgets are great, but I'm struggling to customize the styling. Can anyone lend a hand? ...

Loop through each input element in the form to retrieve the Name and Value using Javascript

I have been honing my JavaScript skills lately, after relying heavily on JQuery in the past. Though I am quite experienced with jQuery, my knowledge of raw JavaScript is limited. I decided to take on the challenge of learning pure JavaScript just like I ...

Save the outcome of an ArrayBuffer variable into an array within a Javascript code

I'm in the process of developing an offline music player using the HTML5 FileReader and File API, complete with a basic playlist feature. One challenge I'm facing is how to store multiple selected files as an ArrayBuffer into a regular array for ...

How to make a range slider using HTML

I am currently working with a range slider that displays only one value, but I want to show two limits - the minimum and maximum values. This is similar to a price range slider where users can select a range of prices. I need to store both the min and max ...