Android之网络数据存储

简介:




一、网络保存数据介绍

可以使用网络来保存数据,在需要的时候从网络上获取数据,进而显示在App中。

用网络保存数据的方法有很多种,对于不同的网络数据采用不同的上传与获取方法。

本文利用LeanCloud来进行网络数据的存储。

LeanCloud是一种简单高效的数据和文件存储服务。感兴趣的可以查看网址:https://leancloud.cn/。关于LeanCloud的数据存储使用方法可以在里面找到,本文不讲述关于LeanCloud的使用,知识借助LeanCloud平台举一个在网络上存储数据的例子。

二、使用方法

1.上传数据

        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. 读取数据

        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);
                }
            }
        });

三、小案例

1.添加strings.xml文件

    <string name="network">Network</string>
    <string name="get_data">获取数据</string>
    <string name="put_data">上传数据</string>

2.修改activity_main.xml文件

<?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.添加NetworkDBManager类

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.修改AndroidManifest.xml文件 

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

5.修改MainActivity

 android.net.*  MainActivity  AppCompatActivity  "MainActivity", "onCreate", "yMNUazdBt872mNtC9aSakjYy-gzGzoHsz", "d4vw3VYdMCjLpsXRhHTBRutC"= === =  Person("xiao", 23, "women"=  Person("zhao", 24, "men""MainActivity", "default"



本文转自lzwxx 51CTO博客,原文链接:http://blog.51cto.com/13064681/1944374

相关文章
|
1月前
|
数据库 Android开发 开发者
构建高效Android应用:采用Kotlin协程优化网络请求处理
【2月更文挑战第30天】 在移动应用开发领域,网络请求的处理是影响用户体验的关键环节。针对Android平台,利用Kotlin协程能够极大提升异步任务处理的效率和简洁性。本文将探讨如何通过Kotlin协程优化Android应用中的网络请求处理流程,包括协程的基本概念、网络请求的异步执行以及错误处理等方面,旨在帮助开发者构建更加流畅和响应迅速的Android应用。
|
3月前
|
安全 API Android开发
Android网络和数据交互: 解释Retrofit库的作用。
Android网络和数据交互: 解释Retrofit库的作用。
38 0
|
3月前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
22 0
|
3月前
|
存储 安全 Android开发
Android数据存储:请解释ContentProvider是什么,它的主要作用是什么?
Android数据存储:请解释ContentProvider是什么,它的主要作用是什么?
25 0
|
3月前
|
存储 Java 数据库
Android数据存储:什么是Room Persistence Library?
Android数据存储:什么是Room Persistence Library?
42 0
|
3月前
|
存储 数据库连接 数据库
Android数据存储:解释SQLite数据库在Android中的使用。
Android数据存储:解释SQLite数据库在Android中的使用。
41 0
|
4月前
|
XML 物联网 API
Android Ble蓝牙App(五)数据操作
Android Ble蓝牙App(五)数据操作
|
8天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android&#39;s AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
9 0
|
8天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
9 0
|
22天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
23 1