How to cache page in jquery mobile for android - android

I am working on a mobile application with phonegap and jquery mobile for android. It is working properly but it takes so much time to get data from server.
I want to cache its pages so if user come back on same page he can view same page.
I am using jquery mobile caching code but it is not working.
I am using this code data-dom-cache="true" for cache
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.appView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
super.loadUrl("file:///android_asset/www/find.html");
}

Cache is not working in my app so I stored all json data in localstorage by doing this
window.localStorage.setItem("ALL_USERS",JSON.stringify(data));
where data is json object I retrieve from server.
And next time i retrieve data from localstorage
window.localStorage.setItem("ALL_USERS",JSON.stringify(data));
users = JSON.parse(users);
This solution solved my problem hope it will help others.
Thanks

Call below code in onCreate() method of your Android application activity file:
super.appView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
The above method uses cache if content is there, even if expired (eg, history nav). If it is not in the cache, load from network
Please look WebSettings constants for cache settings :
http://developer.android.com/reference/android/webkit/WebSettings.html

Related

Webview - Cache webapp data for further (next time use)

we recently received a request as following -
While loading a webapp (https://192.1.../poc/test.jsp) in android or iOS webview. It's taking some time while loading 1st time because of js, image, css etc files takes time to download including some APIs.
Client want to improve the experience and wanted 1st time even takes time and load the .js, images etc offline. When user launch app 2nd time onwards, use the loaded .js, images etc and make only API call from server.
We would like to check if what is the best way to achieving it?
Any out of frame suggestion is also appreciated.
While digging too much, it was solved as following
iOS having in-build property by WKWebView to store supporting web files like (js, css etc) and reuse if needed for further commmunication. Logic to clear cache etc we can manage it manually when it needed.
Android it was webviewclient. Which is having a callback name shouldInterceptRequest(WebView view, WebResourceRequest request)
Everytime any webview page needs css, js etc, this callback to be invoked. So put the logic be like
if (if url extension is js|png|css){
if (response for url is available within app cache) {
return WebResouceResposne object from cache data
} else {
//Save file response in cache &
return to super.shouldInterceptRequest() using interceptor WebResourceResponse
}
}
Hope, it will help someone in anyways. Thank you!

Removing Instagram user data from WebView in Android

I have a Webview in my android project that opens the Instagram URL in a dialog fragment.
I want that everytime the fragment is closed to delete all cashe and information collected by the webview and when I restart it the webview will start fresh with no user information.
I have tried EVERYTHING - nothing works and all documentations about it are very old and not helpful.
thanks!
UPDATE:
after 2 days of heavy digging into this issue I came up with the idea to set the Instagram log-off URL within the onDestroy() method and it magiclly worked. If anyone has any use of it, the url for loggin off is
https://instagram.com/accounts/logout

Xamarin Forms get WebView content as string

Is it possible to get the content of a WebView as a string?
I tried using:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Constants.LOGIN_URL);
HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
But I didn't get what i wanted because the webview stores the sessions etc.
I want a method to get the data of a logged in user from a website if this is possible.
Since you need to download the content of the WebView in the context of a logged in user, you may want to try injecting JavaScript into the WebView and then having the JavaScript send code back to a C# method.
An example of something like this can be found in XLabs' Hybrid WebView (which is the example I used to call JavaScript from C# methods).
Have a look at the URL below. You will need to create a custom renderer for each platform that you support but that should just be a matter of copying and pasting from the link below.
Once you have you custom renderer all set, you can simply pull all of the HTML using a JavaScript method.
https://github.com/XLabs/Xamarin-Forms-Labs/wiki/HybridWebView
Reply back if you run into any road blocks.

get method dosen't return a right result

I'm tring to build a android client for a website, of which I have no control. I can only use httpwatch to detect the data flow and decide to use either post method or get mathod.
Here I come across a problem, after logining in the website, I click a button to load some data(the url in the browser dosen't change,name it url-1), it works well and the httpwatch tells it is using get method with a target url(a different one,name it url-2). If I paste the url(url-2) in the brower it returns to the login page without loading any data. I check the html source code and find the iframe tag, which may be used to realize local refresh.
In android, I use httpclient to do post or get, I can login in and keep a connection with the server, but I failed when tring to use get method to load some new data, it also returns the login page.
Why it works like this, is there any way to get the right answer?

Phonegap ChildBrowser Issue

I am creating a mobile app which connects to some jive based community using childbrowser.
I can easily open the community, login and do stuff.
But I also need to fetch session details in my app, so that I could use these to fetch JSON strings to show user image/info on my app showing that user is logged in.
I tried working on childbrowser, but couldn't investigate much.
It seems it won't work with my community URL. While using google url it shows popups on location changes, but not for my url.
Can any one help me in fetching the data I want ?
Here is the sample code I am using to detect location change:
function openURL() {
window.plugins.childBrowser.onLocationChange= function(loc){
alert('In index.html new loc = ' + loc);
};
window.plugins.childBrowser.showWebPage("https://communityName.jive-mobile.com/#jive-login", { showLocationBar: false });
}
I am getting my login page, but after login I can't see any alert coming !!
I need to get proof of user login and user session details.
Thanks
I have just updated the ChildBrowser plugin for my own use, following code given here: How do I get the web page contents from a WebView?
I am using this to log in on Google API oAuth2, watching for the right page to load. I am using the onPageLoaded(html) event that has the (computed) html source. You might want to use this to get the html that was loaded and check for "stuff". I don't use jive so I wouldn't know where to start, but I'll put up a sample Google oAuth2 project for you to try.
HTH
EDIT:
the code is at: https://code.google.com/p/filechunkreader/
Don't let yourself get fooled by the name, this project is the repo for my Java plugins experiments. It contains so far 2 plugins, FileChunkReader and a custom version of ChildBrowser.

Categories

Resources