I have used the following code:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
It is working fine. But, when the user restarts the application, the cookie gets deleted, and I can't use it for further rest calls.
How can I store the cookie permanently in the Android app?
There are multiple ways using which you can save data permanently (until you can'y clear app data).
SharedPreference
Local Database (SQLite / Room - Recommending)
Using above listed methods you can store you data and also get those data after restart of application/mobile phone.
You can get an idea of access of above listed methods using below links.
SharedPreference : https://www.tutorialspoint.com/android/android_shared_preferences.htm
SQLite : https://www.geeksforgeeks.org/how-to-create-and-add-data-to-sqlite-database-in-android/
Room : https://www.geeksforgeeks.org/how-to-perform-crud-operations-in-room-database-in-android/
Thank you.
Related
In an Android project I use Firebase with signInAnonymously() and I am getting userId like this
userId = FirebaseAuth.getInstance().getCurrentUser().getUid()
and I use the userId to create children nodes in Firebase Realtime Database that only this user can access based on the access rules of that database.
The problem is that I noticed userId changes randomly and when that happens all content created by that user is lost to them. Is there something I can do to keep the same userId until the app is uninstalled? What other way can I use to ensure steady and exclusive access for that user to a Realtime database child? Can installation id be used?
You can use Firebase Anonymous Authentication to create and use only temporary accounts to authenticate with Firebase. The Anonymous Authentication accounts don't persist across application uninstalls. When an application is uninstalled, everything that was saved locally will be wiped out, including the anonymous auth token that identifies that account. So there is no way you can reclaim that token for the user. So each time you sign in, a new UID is generated.
What other way can I use to ensure steady and exclusive access for that user to a Realtime Database child?
To always have the same UID, then you have to implement the Firebase Authentication with a provider like Google, Facebook, Twitter, and so on.
I am developing a menu app for restaurant , I want the user to be able to add a new dish (new row in a table) and when the row is added to the database all other users (who have the app in their device)can see the new update.
I want to use SQLite. is that possible if yes, how?
For such kind of scenario, you need to store the new menu item from a user on backend database (a database which is hosted on an external server) of your application, and all the other users using the application will get that data by calling a web call to your backend.
I am new in android and developing an android application in which admin can insert data on server and users can load that data on their devices. I don't want to load data from server every time user land on that activity. I want to save fetched data at client side using greendao and load only the new one. And i think i have to hit the backend at least once. I am looking for a most convenient method to achieve this.
My preferred way would be to track the device on the server side:
for example create a table like cachedDevices(deviceId,dataId). then whenever you are requesting new data, your server could query your data against cachedDevices internally and only send new data...
you should also be aware of that each device should have unique Id...
I am working on app running on two tablets, which have two type of logins. Customer and Shopkeeper, it will run on two tabs at same time. I am getting tabs device id and saving it. because it can't be run on any other tablet without registered tablets.The issue is that after login when I go on main screen, when i enter number from any tablets, the next screen must be changed on both tablets. I am not getting the idea how to operate two tabs from any one of them.Can you help me to get this idea. any help will be appreciated.
I have 2 separate options here :
A. Use CouchDB.
Couch DB has this functionality to sync data between client and server.
Steps :
Tablet1 enter a value
Value saved in tablet1 CouchDB. (You can use something like couchbase-android)
Data synced from client COUCHDB to Server CouchDB ( you can use Cloudant here )
Data synced from Server CouchDB to client CouchDB in the Tablet1
Tablet2 use a listener to listen to record change in the database
Tablet2 change its state based on the record change.
B. Use Notification Server
Tablet1 enter a value
Tablet1 send message to Notification Server, indicating the changes
Notification Server send notification to Tablet2
Tablet2 use Notification Listener to listen to all notification
Tablet2 change it state based on data contained in the notification message.
During the Application startup , using the login Id of the User , i am making a Database call and loading all of his accounts and setting them in the session as shown
session.setAttribute("userinfo",userinfo);
and i am using this accounts information in the service layer to do a check before making a call to the Database
Now the problem is that if a User (who is having multiple accounts ) logs simulatunosly into a same browser , its creating same sessionid , as a result the session is having only the information of the last logged in user .
is there anyway i can solve this , may be the way i am storing data
please help
May not be the best solution but this will help to solve the problem.
What you could do would be to associate the userid with the sessionid and on every pageload / clicks, you will check if the sessionid matches the login user's userid. If it matches, disregard and run the page as usual else you can fetch the user info from the database and reassign the variables again.
You can use URL rewrite instead of cookies. The downside is that the session ID is exposed in the URL.