Android Network Data Storage and android Data Storage

Source: Internet
Author: User

Android Network Data Storage and android Data Storage
1. Introduction to saving Network Data

You can use the network to save data, obtain data from the network as needed, and then display the data in the App.

There are many ways to store data over the network, and different upload and retrieval methods are used for different network data.

This document uses LeanCloud to store network data.

LeanCloud is a simple and efficient data and file storage service. For more information, see https://leancloud.cn /. You can find how to use LeanCloud data storage. This article does not describe how to use LeanCloud. The LeanCloud platform provides an example of how to store data on the network.

Ii. Usage 1. Upload data
        AVObject personObject = new AVObject(TABLENAME);        personObject.put(NAME, person.name);        personObject.put(AGE, person.age);        personObject.put(INFO, person.info);        personObject.saveInBackground(new SaveCallback() {            @Override            public void done(AVException e) {                if (e == null) {                    Log.v(TAG, "put data success!");                } else {                    Log.v(TAG, "put data failed!error:" + e.getMessage());                }            }        });
2. Read data
        AVQuery<AVObject> avQuery = new AVQuery<>(TABLENAME);        avQuery.findInBackground(new FindCallback<AVObject>() {            @Override            public void done(List<AVObject> list, AVException e) {                if (e == null) {                    Log.v(TAG, "get data success!");                    String message = "";                    for (int i = 0; i < list.size(); i++) {                        String name = list.get(i).getString(NAME);                        int age = list.get(i).getInt(AGE);                        String info = list.get(i).getString(INFO);                        message += "name:" + name + ",age:" + age + ",info:" + info + ".\n";                    }                    textView.setText(message);                }            }        });
Iii. Case 1. Add the strings. xml file
<String name = "network"> Network </string> <string name = "get_data"> obtain data </string> <string name = "put_data"> upload data </string>
2. Modify the activity_main.xml File
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.zhangmiao.datastoragedemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/network" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/fab_margin" android:layout_marginTop="@dimen/fab_margin" android:orientation="horizontal"> <Button android:id="@+id/network_put" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/put_data" /> <Button android:id="@+id/network_get" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/get_data" /> </LinearLayout> <TextView android:id="@+id/table_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> </LinearLayout></android.support.design.widget.CoordinatorLayout>
3. Add the NetworkDBManager class
package com.zhangmiao.datastoragedemo;import android.util.Log;import android.widget.TextView;import com.avos.avoscloud.AVException;import com.avos.avoscloud.AVObject;import com.avos.avoscloud.AVQuery;import com.avos.avoscloud.FindCallback;import com.avos.avoscloud.SaveCallback;import java.util.List;/** * Created by zhangmiao on 2016/12/22. */public class NetworkDBManager {    private static final String TAG = "NetworkDBManager";    private final static String TABLENAME = "person";    private final static String NAME = "name";    private final static String AGE = "age";    private final static String INFO = "info";    public void putData(Person person) {        AVObject personObject = new AVObject(TABLENAME);        personObject.put(NAME, person.name);        personObject.put(AGE, person.age);        personObject.put(INFO, person.info);        personObject.saveInBackground(new SaveCallback() {            @Override            public void done(AVException e) {                if (e == null) {                    Log.v(TAG, "put data success!");                } else {                    Log.v(TAG, "put data failed!error:" + e.getMessage());                }            }        });    }    public void getData(final TextView textView) {        AVQuery<AVObject> avQuery = new AVQuery<>(TABLENAME);        avQuery.findInBackground(new FindCallback<AVObject>() {            @Override            public void done(List<AVObject> list, AVException e) {                if (e == null) {                    Log.v(TAG, "get data success!");                    String message = "";                    for (int i = 0; i < list.size(); i++) {                        String name = list.get(i).getString(NAME);                        int age = list.get(i).getInt(AGE);                        String info = list.get(i).getString(INFO);                        message += "name:" + name + ",age:" + age + ",info:" + info + ".\n";                    }                    textView.setText(message);                }            }        });    }}
4. Modify the AndroidManifest. xml file
  <uses-permission android:name="android.permission.INTERNET" />  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
5. Modify MainActivity
package com.zhangmiao.datastoragedemo;import android.content.ContentResolver;import android.content.ContentValues;import android.database.Cursor;import android.net.*;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.avos.avoscloud.AVOSCloud;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private NetworkDBManager mNetworkDBManager;    private TextView mTableInfo;    @Override    protected void onCreate(Bundle savedInstanceState) {        Log.v("MainActivity", "onCreate");        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        AVOSCloud.initialize(this, "yMNUazdBt872mNtC9aSakjYy-gzGzoHsz", "d4vw3VYdMCjLpsXRhHTBRutC");
mNetworkDBManager = new NetworkDBManager(); Button networkGet = (Button) findViewById(R.id.network_get); Button networkPut = (Button) findViewById(R.id.network_put); mTableInfo = (TextView) findViewById(R.id.table_info); networkGet.setOnClickListener(this); networkPut.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) {case R.id.network_put: Person person3 = new Person("xiao", 23, "women"); Person person4 = new Person("zhao", 24, "men"); mNetworkDBManager.putData(person3); mNetworkDBManager.putData(person4); break; case R.id.network_get: mNetworkDBManager.getData(mTableInfo); break; default: Log.v("MainActivity", "default"); break; } }}

Code: https://github.com/ZhangMiao147/DataStorageDemo

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.