Xamarin android WebClient Json downloads and stores local and sqlite databases, xamarinsqlite
This skill may not be worth mentioning to anyone who is familiar with it. But I think, since these are common functions, it also makes sense to integrate them together to take notes.
First, json is the de facto standard for data transmission. So let's start by downloading it from the server .. Net Library is excellent, which is one of the reasons why I like Xamarin. A WebClient can download any data.
System.Net.WebClient wc = new System.Net.WebClient();string s = wc.DownloadString(http://192.168.0.100/data.json);
Of course, the server must have this file or a webpage that can return json data.
Next, what if I want to save this file locally? You can also use System. IO. File in one sentence. System. IO. File. WriteAllText ("localdata. json", s );
Note that local files are stored with permissions. Therefore, it is best to save the data to the private space of the application and ensure security, as shown below.
string fn = System.IO.Path.Combine(System.Environment.GetFolderPathSystem.Environment.SpecialFolder.Personal) ,"localdata.json");System.IO.File.WriteAllText(fn,s);
If the database content is not involved, the end is now.
However, there is a lot of need to save the server-side data to the local database. This is convenient and fast to use. The most common database in Android is SQLite. The corresponding database is SQLite.net. Which is also excellent and easy to use. At the same time, it is also an ORM, so it satisfies the taste of the vast majority of people.
When using it, you must reference its Package and directly install-package. But before that, I want to convert the json data into an object and store it in the database for convenience. Therefore, the json library is referenced first.
install-package Newtonsoft.json
It automatically downloads the latest version that matches the current project. The current version is 10.0.2. Then write the code and compile it.
Something went wrong. Compilation failed! The library it depends on may not match the Xamarin android profile.
After a long time, we finally installed version 6.0.0 and compiled it. I don't know why.
After adding a reference, you need to use it. First, I deserialize the preceding json file into an object list. Here I define a User class.
List<User> list= JsonConvert.DeserializeObject<List<User>>(s);
It was a great success.
Next, you need to reference the SQLite.net library. This was a good time and nothing went wrong.
Install-Package sqlite-net-pcl
Sqlite-net-pcl is used. Is it used for mobile devices?
Next, create a table.
var cnn= new SQLite.SQLiteConnection(Helper.PrivateFileName("db")); cnn.CreateTable<User>();
You must first create a SQLiteConnection object. The parameter is the name of the local database file. Create a table using CreateTable <T>.
Next, insert the data into the table.
cnn.InsertAll(list);
Next, query all the data.
List<User> users = cnn.Table<User>().ToList();
The code is so concise, so beautiful, almost all of them are a sentence.
What is touching is the C # And. net libraries, which reveal the concept of simplicity and supremacy everywhere. They like this style very much. Unlike some languages, simple problems are always complicated. You have to define everything as a class.
Let's first come here today. Let's talk about more details later.