LeanCloud getting started (android), leancloudandroid
LeanCloud is a simple and easy-to-use ECs, which includes powerful database support. We only need to apply this server to the local code to achieve storage and interaction in the background.
Then, how can we easily implement the interaction between local code and the LeanCloud server? The following is a detailed explanation:
First, the development environment: Android Studio 1.5 (hereinafter referred to)
Step 1: use AS to create a project. The steps are AS follows,
Next, click finish to complete the creation.
Step 2: Enter the LeanCloud official website for Baidu search and register for Logon (I will log in directly here). The interface is as follows:
Step 3: Create an application on the server and click "create application". "apsaradb" is the name of the application I named. (Note that the name can be written as needed, it has nothing to do with the application name of the local code)
Click Create. The following page is displayed, so that you have successfully created an application on the server.
Now we can click storage to see the initial database, and the content is empty.
This application is unique, because at this moment it has been matched with a specific App ID and App Key to specifically identify the application. We can click the settings in the upper right corner to enter the App Key, as shown in
Step 4: Download the encapsulated sdk
Click Download SDK in the lower right corner of the page to go to the page.
Because we only use simple server connections, we only need to download the Basic module.
Step 4: Decompress the downloaded compressed package file (note that you must decompress the package because it is still a package). decompress the package and obtain the following five jar packages:
Copy and paste them to the libs directory of the AS project,
The file under the imported package is not displayed, indicating that it has not been applied to our program. Then, click the compile button in the AS menu bar.
All the folders in the package are displayed, and the import is successful.
In this case, we need to add some Internet request permissions in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
Step 5: Call AVOSCloud. initialize in the onCreate method of the Application to set the Application ID and Key:
In this case, we create a package app to facilitate interaction, and create a class MyApplication under the package to inherit the Application to process the code,
Register this class in the AndroidManifest. xml file
Add a line of code to the onCreate () method of MyApplication,
AVOSCloud. initialize (this, "2WGq1OsoGNQAlUe9CbAnBJfO-gzGzoHsz", "f4qvMLMQPzAPPVzd41797RG6 ");
It is used to set the Application ID and Key, so that the local application can interact with the background apsaradb one-to-one. For example
Here, 2WGq1OsoGNQAlUe9CbAnBJfO-gzGzoHsz is the app id we opened earlier.
F4qvmlmqpzappvzd000097rg6 is the App Key that was previously opened. Copy and paste the two files here.
Step 6: test real interaction
Add the following code to the onCreate () method of MainActivity (note that the name must be in English only)
AVObject testObject = new AVObject ("testObject"); // create a table named testObject in the background
TestObject. put ("name", ""); // Add a column to the table named name.
TestObject. saveInBackground (); // save it in the background
After you click Run project, the following error is returned:
The error message is: The contents of the META-INF file in the two jar packages are repeated.
Add several lines of code to the build. gradle file to ignore the file.
PackagingOptions {
Exclude 'meta-INF/LICENSE.txt'
Exclude 'meta-INF/NOTICE.txt'
}
Location
Click "run" again, and the project runs properly.
Step 7: View background database changes
We can see that a table named testObject is automatically created in the background, a row of data is added, and a column of name is added. In this case, all interactive operations are completed.