Android WebView cache doesn't expire - android

in my Android App I use a WebView and got a problem with caching.
After a file is cached the WebView just keeps it forever (or at least until I delete the cache manually).
In the Apache Access-Log I see that the WebView doesn't send requests once the file is cached. How can I setup the WebView to send requests for checking if the file is expired by using common ETags and HTTP-Code 304? On a desktop browser(Firefox, Chrome) the caching works as expected.
Here is my code:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new EWebViewClient(this));
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.setWebChromeClient(new EWebChromeClient(this));
webView.setBackgroundColor(Color.argb(1,0,0,0));
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(url);
EWebViewClient only overwrites: "shouldOverrideUrlLoading, onPageFinished, onLoadResource, onReceivedError"
I really appreciate any help you can provide.

Related

Android WebView is taking more time to load website then the browser in laptop, both laptop and android mobile are using same internet connection

Is there something I am missing to set some property of webview?
I have set following property but no improvement in performance:
webview.setWebViewClient(new myWebClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
// Enable the caching for web view
webview.getSettings().setAppCacheEnabled(true);
// Specify the app cache path
webview.getSettings().setAppCachePath(getCacheDir().getPath());
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Any help will be highly appreciated, Thanks in advance.
Actually you are using webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); which disallows any kind of caching in your Android WebView.
While browsing in browser, most of your content is loaded from cache, which results in difference of loading time. Hence, affects overall performance.
You should use it as follows to improve caching:
webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

Android Webview doesn't load

I have a problem with Android Web view.
The slider work fine with Apple but doesn't load on Android WebView.
Can anyone help? www.pgc-usa.net/ivynails
Thanks
I think I know. Web views in Android by default do not have JavaScript enabled. This is because there is someway it can do harm to the app. To enable it though it takes two lines
WebSettings webSettings = yourWebview.getSettings();
yourWebview.setJavaScriptEnabled(true);
You can also set it so that links that are clicked open in the web view. ( Otherwise they open in browser.) to add this you just add
yourWebview.setWebViewClient(new WebViewClient());
All that together will make it come to this:
WebView yourWebview = (WebView) findViewById(R.id.myWebview);
yourWebview.loadUrl("http://www.pgc-usa.net/ivynails");
WebSettings webSettings = yourWebviewgetSettings();
webSettings.setJavaScriptEnabled(true);
yourWebview.setWebViewClient(new WebViewClient());
You can find out more by visiting this page on the Android developer site by clicking this link.

Mozilla PDF extremely slow on Samsung Devices

I have an application in which I am using Mozilla PDF for viewing PDFs.
Mozilla PDF uses web view to load PDFs.
The PDF files are large, and are all images actually.
The application works fine, but the PDF loads extremely slow on all Samsung devices. The application works fine on other devices, but is so slow on Samsung that it's really useless.
I have the following settings for the web view:
settings.setJavaScriptEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I also have set a WebChromeClient.
Does anyone knows why am I facing this issue? And only on Samsung devices?
Any workarounds? Any help would be greatly appreciated.
Here’s a WebView setup to display PDFs using PDF.js. It works fine on all devices (that are running at least API 19), including old tiny about-to-die Samsungs.
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccessFromFileURLs(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
// should enable pinch to zoom....
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
String webViewUrl;
try
{
/*
Try encoding the PDF file URL. I think i’ve read that this helps with
load/rendering speed?
*/
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder
.encode(pdfUri.toString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// It works without encoding it (at least in my case) so just display it with the string as is.
Log.e("Error encoding PDF file URL, will display without encoding.", e);
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUri.toString();
}
webView.loadUrl(webViewUrl);
Not sure if this will help, but thought I’d post a full WebView setup to support PDF.js, which I’ve never run into speed issues with. Good luck.
Note: Might also be a good idea to turn hardware acceleration on in your application’s manifest. This is only available on API 14 and above. By now this shouldn’t be an issue (if it is, my heart goes out to you).
<application
android:name="com.pdfjs.webview.app.McAwesome"
//...
android:hardwareAccelerated="true">
</application>

Disconnect the already loaded webpage(uses socket.io) before loading the new url in WebView

The WebView loads a webpage which uses socket.io
WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(socket_io_page_url);
The session is maintained using cookies. When the android logout action item is activated the cookies must be cleared and a new url is loaded as shown in the following code
WebView myWebView = (WebView)findViewById(R.id.webView);
CookieManager.getInstance().removeAllCookie();
myWebView.loadUrl(new_url);
This works but the connection to the previous url(socket_io_page_url) is not terminated immediately. Since the WebView does not support web sockets, the long-polling is used by the webview and it waits about 20 seconds to disconnect the previous url. The time out can be changed, but it will be a very bandwidth costly operation.
At this point it is not possible to switch to native java coding. So disconnecting the previous connection is very important to the application. I tried
myWebView.destroy()
System.gc()** after new url load,
webSettings.setJavaScriptEnabled(false); and setting back
webSettings.setJavaScriptEnabled(true);
None of them worked. killing the application using cleaner application disconnects the socket immediately. Is there a way to disconnect the previously loaded page.

android - Webview not showing content properly of Facebook URL

I have an application in which I'm passing this URL to WebView.
Facebook URL
But the WebView is not displaying the page correctly.
The default android browser is displaying the content correctly.
Code
WebSettings wsettings = webView.getSettings();
wsettings.setBuiltInZoomControls(false);
wsettings.setSupportZoom(false);
wsettings.setJavaScriptEnabled(true);
webView.setScrollbarFadingEnabled(false);
webView.setHorizontalScrollBarEnabled(true);
wsettings.setUseWideViewPort(true);
wsettings.setLoadWithOverviewMode(true);
wsettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
wsettings.setAppCacheEnabled(true);
wsettings.setAppCachePath("");
wsettings.setUserAgentString("AndroidWebView");
wsettings.setDatabaseEnabled(true);
wsettings.setDomStorageEnabled(true);
webView.clearCache(true);
Please see the below screen shot for reference.
I have found the solution for this by removing the below code.
wsettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
wsettings.setAppCacheEnabled(true);
wsettings.setAppCachePath("");
wsettings.setUserAgentString("AndroidWebView");
wsettings.setDatabaseEnabled(true);
wsettings.setDomStorageEnabled(true);
webView.clearCache(true);
Add this options in your WebView code:
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT < 8) {
mWebView.getSettings().setPluginsEnabled(true);
} else {
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
In my case, Facebook works perfectly.
Regards

Categories

Resources