Accessing stored data in Android webview when server connection is unavailable

I have an Android application that utilizes a webview. The backend server is written in Java and serves all the front-end files, creating the cache manifest through Apache Tomcat.

My predicament lies in figuring out how to make the webview load from the app cache when the server connection is unavailable, but the device still has internet access.

By manually adjusting the app code, I am able to instruct the webview to exclusively use the cache with:

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

This ensures that my files are being properly cached. I've also attempted solutions found on stack overflow where network status is assessed and the cache mode is set accordingly based on network availability:

cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
if(cm.getActiveNetworkInfo().isConnected()) ....

Nevertheless, this method fails when the device is connected online but my server is down. In such cases, it inaccurately detects a functioning network and does not resort to loading from the cache.

The ideal scenario would be for the webview to automatically switch to loading from the cache if a resource cannot be accessed over the network, similar to how a standard browser behaves. However, I'm open to alternative approaches, like having the app recognize the network connection specifically to my server and adapting the cache mode accordingly.

If anyone can suggest the most effective solution for this issue, I would greatly appreciate it. Thank you.

Answer №1

It was discovered that android does not automatically utilize the HTML5 app cache unless specifically instructed to do so, which caused some confusion in troubleshooting. The issue was resolved by implementing the following code:

final WebSettings webviewSettings = webView.getSettings();
final String appCachePath = this.getCacheDir().getAbsolutePath();
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setAppCachePath(appCachePath);
webviewSettings.setAllowFileAccess(true);
webviewSettings.setAppCacheEnabled(true);
webviewSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

After applying these changes, the app cache began functioning as expected. This means that resources are loaded from the cache if they are unavailable over the network, according to the specifications outlined in the CACHE section of the cache manifest.

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

Ways to halt the execution of a setTimeout function within a loop

This question is a follow-up from this thread - setTimeout inside a loop, stops script from working I'm facing an issue with my script that fetches data from an API and stores it in a MongoDB collection. The problem seems to be related to the setTime ...

Steps for creating a vector list or an array of lists in Java

How can I create a vector array like this in both C++ and Java? vector<int> tab_u[255] I've tried the following method, but it doesn't seem to work: List<Integer> tab[] = new List[255]; My desired output would look something like ...

Attempting to reduce the width of the dig when it reaches 400

I have a square element with a click event that increases its size by 50 pixels on each click. However, once it reaches a size of 400 pixels, the size decreases by 50 pixels with every click instead. Additionally, when the size of the square reaches 100 p ...

Is there a way to retrieve data from three different Bookshelf.js models in a single query?

Three tables are in my database: User, UserDrink, and VenueDrink Here is a preview of the sample data: User id | name | gender | -------------------- 1 | John | male | 2 | Jane | female | UserDrink id | count | user_id | venue_drink_id 1 | 3 | ...

Click to be redirected to the same page on a different store

I have been working on a Shopify project where I implemented a pop-up that appears when the page loads, prompting users to choose between the US and UK websites. Once a user clicks on one of the options, the pop-up either closes (UK) or redirects them to ...

Elements are unresponsive to scrolling inputs

My Ionic 2 input elements are not scrolling to the top when the keyboard is shown. I've tried everything I could find on Google, making sure the keyboard disable scroll is set to false. However, I still can't figure out what's causing the sc ...

How to achieve a successful response with Ajax and jQuery?

I'm currently working on practicing data retrieval through an API using jQuery. After watching a few YouTube tutorials, I still can't seem to get a successful response... it's quite frustrating. Aside from YouTube, I'm also a subscribe ...

Can browserindocker() or watch() handle the recording of browser executions in headless mode within a Docker environment?

Is it possible to video record browser activities during container execution using the APIs browserindocker.enableRecording() or watch() in Boni gracia webdriver manager 5.6.2? I have been attempting to capture video recordings of browser activities withi ...

Is JavaScript still running despite not displaying insecure items due to IE 8 security warning?

After a user completes a transaction on my site, the confirmation page displays Google conversion tracking code in a small JavaScript snippet. This code is located on my Wordpay callback page, which pulls data from the regular site (HTTP) to the Worldpay s ...

Can Redux enable real-time data synchronization across various devices?

In an office setting, there are two computer systems running a shared web application. One system is assigned to the employee while the other is for the admin. When the employee logs in, they see their schedule on their dashboard. Conversely, when the admi ...

The Lightgallery plugin is creating three duplicates of the slides

I am currently facing an issue with a gallery that loads images from an API and displays them using the lightgallery plugin. Upon implementing the lightbox in the correct location (view question here), I discovered that the plugin is generating three slid ...

Can a websocket be used to communicate with a server that is not the same as the one hosting the html5 website?

My goal is to establish communication between a hardware device and an HTML5 website. It seems like the best way to do this would be through WebSockets or possibly WebRTC over a WiFi network. Is that correct? I haven't come across any solutions invol ...

How to display content in separate divs using jQuery hover functionality

I'm in the process of creating my online portfolio by using bootstrap and jquery. I have a series of images arranged side by side with each other, and I want to make the description for each image appear when that specific image is hovered over. Each ...

Using Javascript on the client side to generate fresh images using an image already present on a webpage

Is there a way to slice an image into tiles using JavaScript in a web browser? I want to provide users with the option to choose an image from a gallery on a webpage and then divide that image into 4 or 6 new tiles. The tiles would then be displayed in a ...

Object Literal vs Object-Oriented Javascript: Comparing the Two

When it comes to using Object-Oriented Programming (OOP) in JavaScript, I often find myself not utilizing it much. For instance, instead of defining a constructor function and setting up prototypes like this: function Person(name){ return this.name = name ...

What are the potential outcomes of using an empty layout with a background color on an Android platform?

What changes will occur to the background color of the Layout shown below: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:la ...

Setting the Holo Light theme for all Android applications: A step-by-step guide

By mistake, I altered the layout and now all my activities have a dark color with white text "inside TextViews", whereas before they were grey with black text. After researching online, it seems like I accidentally changed all activities from "Holo light" ...

What could be the reason behind jQuery replacing the entire span with .text?

I am dealing with the following HTML code snippet: <p><input id="revenue" type="text" value="100000" /><span id="howmuch"><a href="" class="ka_button small_button small_cherry" target="_self" style="opacity: 1; "><span>How Mu ...

Creating a selectAll checkbox that triggers the ng-click function on each individual checkbox

I've encountered an issue with my code where clicking a checkbox triggers the ng-click function. Here is the JavaScript snippet: $scope.selectTitle = function(evt, selected){ evt.stopPropagation(); var filtered = _.findWhere($scope.se ...

Challenges associated with using GridBagLayout

Having trouble transferring related posts to my simple problem, so I decided to create a basic example using GridBagLayout: JFrame frame = new JFrame(); JPanel main =new JPanel(); frame.setContentPane(main); GridBagLayout gbl=new GridBagL ...