Use Gson and SharedPreferences to easily build android local data cache

Source: Internet
Author: User

I have just read an IBM blog about the Android local data storage API, which is too complicated. For most individual developers, follow this method to create their own projects, it is really a very painful thing. As a believer in Agile development, it is really hard to bear to do things according to the set of IBM, the following describes a set of data storage methods that I have summarized in practice. Local data storage for Android is nothing more than 1 using SharedPreferences to store data 2 file storage data 3 SQLite database to store data 4 using ContentProvider to store data has been corrupted, first, there are many storage user settings, and the second is used in image cache game data mixing villages. The third is the most powerful database function. Combined with some ORM classes, A wide range of applications can be built, and a lot of data that requires interaction with the server will go through SQLite. According to the traditional Java habits, SQLite is the easiest way to think of data from multiple objects. However, to use SQLite, you need to write a DBHelper by yourself, this is natural for those with years of Java Database experience, but in actual Android project development, you need to focus on the UI, user experience, products, and logic, it is difficult to have the energy to implement such a set of things on your own. Even if you are lucky enough to do so, you will encounter various troubles during use, in short, using the database locally is really a thankless task. Of course, if you simply use SharedPreferences, you can only save some key-value pairs, which is too troublesome for Android platforms with rich UI interaction and data interaction, I have been plagued by this problem, and it is always difficult to develop Android. Another problem that bothers me is that JSON data returned by the server often needs to be parsed. A set of JSON parsing tools is always required because different projects interact with different data, JSON objects are often different, which is a headache. However, since the access to Google's Gson, there has been a qualitative leap in the efficiency of JSON processing. It turns out that there are still such a great tool, and it will be a nightmare. Looking back at the local data cache of the Android client, and then looking at the JSON data, I picked up Gson and saved the object to SharedPreferences as a JSON String, the JSON String is instantiated as an object using Gson, And the setText (user. name), is there a better way than this? The following are some code snippets used in practice:

public class UserUtils {      public static String getUserInfo() {          SharedPreferences pref = getAppContext().getSharedPreferences(                  "user_login", 0);              return pref.getString("user_info", "");      }      public static boolean saveUserInfo(String info) {          SharedPreferences pref = getAppContext().getSharedPreferences(                  "user_login", 0);          return pref.edit().putString("user_info", info).commit();      }  }  

 

If the server returns the JSON data of a User during use, we can:
Gson gson = new Gson (); User user = gson. fromJson (response, User. class); // modify the user variable and save UserUtils. saveUserInfo (gson. toJson (user); // you do not need to access network resources the next time you use it. User user = gson. fromJson (UserUtils. getUserInfo (), User. class );

 

With simple code, you can implement a local persistent cache of object data, which is extremely flexible and highly scalable. Finally, I would like to recommend some very useful Android tools: AsyncHttpClient and Gson. These two things come together and are really so powerful on the Android platform, the former implements a set of Asynchronous Network resource requests and does not need to open new threads by itself. You can go to its official website to see the details, and I will not talk much about the latter, in short, who knows who to use, haha.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.