I'm a bit confused about when and why to use CookieSyncManager. According to the docs, it can be used to manually force syncing the cookies stored in RAM to permanent storage. Can anyone describe a scenario in which this needs to be done?
In my application, I'm using CookieManager to extract cookies out of a WebView I show, after the user successfully logged in a webapp, using the WebView. Should I force a sync before trying to extract these cookies? So far, I was able to get the cookies without syncing, but reading about CookieSyncManager I'm starting to think that I'm doing something wrong.
Related
All WebViews of an Android app share (among other data) their set of stored cookies. I want to separate the cookie storage, i.e., every WebView should have their own set of cookies.
Therefore, I am looking for a way to retrieve all cookies including meta-information (Expire date, flags etc.) from a WebView, save them separately and set them again when needed. CookieManager has a method getCookie(), however, it just returns value and name of currently valid cookies without any meta info (as discussed here).
Android WebView stores cookies in an SQLite3 database on disk. Apparently, the only way to gain the full cookie information is to have a look at the on-disk cookie store. It would be possible to parse the SQLite3 database and use CookieManager's setCookie() but then all encrypted values would be lost. So my idea was to copy the whole database file and exchange (i.e., rename) the file as needed.
Potential problems of this approach:
Synchronization issues: I don't have access to any locks the system may use for access to the cookie file.
Even if I try to rename the file ignoring the first problem, the database is only read upon start of the app. Afterwards, all cookies (both old cookies read from disk and new cookies added during runtime) are kept in RAM. I would need to force a reload from disk, but the API only offers sync in the other direction, i.e., from RAM to disk.
Where can I find the code that loads the cookies from disk initially? Android SDK's CookieManager.java is an abstract class, where do I find the actual implementation? It may have valuable insights on the issue.
For reference:
A member of the Chromium development team confirmed that it is currently not possible to separate the cookie storage within an app.
I'm sorry in advance if this question is not good enough to be asked, But i made a lot of search to get the proper approach to store cookies in my android app which makes a lot of web connections.
I found more approaches deals with storing cookies in android app, But i can't determine what is the proper one, Or when i should use one rather than others !!
I'll write some of what i found not all:-
SharedPreferences approach.
CookieManager approach.
HttpCookie approach.
The most two things i consider about cookies storing are security and long-lived age of cookie.
It depends on how you want to use them:
Only SharedPrferences approcach allows persistent cookie storage. For example if you do not want user perform login every time he runs your app.
CookieManager is used internaly by WebView. It keeps cookies in
class InMemoryCookieStore implements CookieStore
so it is not persistent.
HttpCookie is used by HttpClient. It is just cookie representation and it doesn't responsible for their storage.
I set up Contacts and Synchronization in the application I am developing. Authentication works in directly to the web site using DefaultHttpClient. That means I need to store cookies and every time user does something send it to server using that cookie. What is the better way to store the cookies? It seems CookieSyncManager only works with WebView. Or can I use it with DefaultHttpClient?
In the webview, there is a setting. I think this may help you:
WebSettings.setSavePassword(boolean save);
I have an app need login, I use a singleton http client to do everything, so it can track the cookies for me.
But when I launch a browser intent in my app to view some html pages, the app sometimes be killed by low memory, when user come back from the browser, my app activity would be recreated, but the new http client would not contains that login session id.
So I think what I need is to cache the cookies when my app get killed, and then restore it back when the app got recreated. I know there is a CookieSyncManager, but I do not have a full picture of how to use that.
(1) So How can I do that? is Cookie seralizable, I just thought to cache it in the sdcard, maybe a bad idea.
Another more general question maybe:
(2) How to share httpclient with webview/system browser? Not just pass cookies from httpclient to webiew/browser, but also get the cookies when initialize the cookies, How to make the http client and webview/browser share just ONE copy of cookie store in any time?
Use SharedPreferences to store cookies as string.
Take a look at this; http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
Also android developers use parcelable class, rather than serializable class. I think the reason is performance issues # mobile devices...
I need help figuring out the best, cross-browser compatible way to "SAVE" user input and STORE them locally(offline mod) AND on a server(online). Program will be used by Android and iOS.
I want to know the best way to track user progress while the device is online OR offline.
Hello I have been researching AJAX, JSON, XMLHttpRequest, REST, Java, and HTML5 (specifically, localStorage).
The scenario: (Read a book online/offline, save page progress)
A user logs in to a Web Service and the Web Service allows the user to download an "html webpage book" (view with HTML5 browser).
After every page turn, a REST API uses a GET request to post the Progress data to a Web Server. Simultaneously, a JSON string is created and saved in a file on the server. (let's say "ProgressData.txt")
In the background, a separate "copy" of ProgressData.txt is saved LOCALLY on the mobile device. The user then leaves the internet connection and continues to read the HTML Book.
When the user regains connectivity, the ProgressData.txt is uploaded to the server using a REST API where it will update the old server file with the NEW .txt file with all of the user ProgressData.
Possible solutions:
HTML5 localStorage solution looks good. jQuery even simplifies it:
http://plugins.jquery.com/project/html5Storage
Straight Javascript looks good for Server-Side storage, however it doesn't have access to a mobile device's physical hard-drive, thus preventing any kind of offline saving.
Java applets look possible. Plus not sure how Java runs with Android/iOS.
I don't want to have to run a localhost(PHP/Apache/Python) from a mobiledevice every time the user goes offline, however that may be where the solution lies. I did stumble on this powerful tool: http://couchdb.apache.org/
Question:
I need to know the best way to track user progress while the device is online OR offline. What is the best way to do this?
I stumbled across store.js the other day which might help solve the cross browser local storage. It was from this article about local storage.
I think your best option for tracking online/offline is to ping the server via an AJAX call when the page is turned. Always try and update the server on a page turn, but if it fails, handle the failure and store the progress locally. Each page turn will either amend the locally stored progress file or if connection is restored then simply update the server with the progress.
The issue I am thinking might occur is if a book is finished offline then there are no more clicks that would trigger the syncing, regardless of a restored connection. You may want to think about a manual sync link/button at the end of a book. Or maybe have a manual sync available at all times anyway? Give some control to the users and describe the whole offline/online reading scenario. You might find that it is easier to just let the users do the work… if they don't sync then it's their problem!
Here are 2 screencasts what will help you with your problem.
They are in Ruby on Rails but maybe you can get the idea. It is using the html5 cache manifest.
Hope it will help you!
http://railscasts.com/episodes/247-offline-apps-part-1
http://railscasts.com/episodes/248-offline-apps-part-2
some more resources (sorry i dont have experiences myself with html5 cache manifest)
http://diveintohtml5.ep.io/offline.html
http://developer.apple.com/library/safari/#documentation/iPhone/Conceptual/SafariJSDatabaseGuide/OfflineApplicationCache/OfflineApplicationCache.html
I'd suggest just using a cookie to store the current state. That way it is automatically sent to your server with every user request (so no need to build out a custom server-side API for receiving the state after a lost connection, and no need to have any custom client-side code for sending the data to the server), and still updateable even if the user has lost Internet connectivity. Also it doesn't rely on HTML5 features, so you don't need to restrict people to HTML5-capable browsers.
In any case, the best way to handle storing the current state would be to have a simple onclick handler on your "next page" link (or button, or whatever it is) that calls a function and sets the cookie value to whatever the current position is. Note that because the state is always available client-side, and sent to the server on every request, there is no need to maintain any explicit copy of the state server-side, unless you want to be able to remember the user's place even when they manually delete their cookies (which is overkill in my opinion).
You may want to look at the W3C Example Code for setting/getting cookie values in JavaScript.
Also, here's a website that demonstrates functionality similar to what you want to build. It uses cookies to keep track of a user's place when reading various webcomics. Pretty much the same as what it sounds like you want, except with comics instead of books.
It would be wise to track the progress in both a server side database and in the client's local storage if a constant internet connection is not necessary.
Evercookie is a controversial javascript api that aims to provide local storage using any means available including standard cookies, Flash shared object, Silverlight, browser history and HTML 5 storage. Data should persist when the user is offline and when the connection is restored, sync the cookie and database with whichever data has greater page number for the given book. Droid has Flash and the Flash shared object data is a "cookie" available to both desktop and web-based apps.
With great power comes great responsibility:
http://samy.pl/evercookie/