欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)

 更新時(shí)間:2016年12月23日 17:10:30   作者:潘侯爺  
LeanCloud是一種簡(jiǎn)單高效的數(shù)據(jù)和文件存儲(chǔ)服務(wù),本文主要介紹了利用LeanCloud來(lái)進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的存儲(chǔ)的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧

一、網(wǎng)絡(luò)保存數(shù)據(jù)介紹

可以使用網(wǎng)絡(luò)來(lái)保存數(shù)據(jù),在需要的時(shí)候從網(wǎng)絡(luò)上獲取數(shù)據(jù),進(jìn)而顯示在App中。

用網(wǎng)絡(luò)保存數(shù)據(jù)的方法有很多種,對(duì)于不同的網(wǎng)絡(luò)數(shù)據(jù)采用不同的上傳與獲取方法。

本文利用LeanCloud來(lái)進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的存儲(chǔ)。

LeanCloud是一種簡(jiǎn)單高效的數(shù)據(jù)和文件存儲(chǔ)服務(wù)。感興趣的可以查看網(wǎng)址:https://leancloud.cn/。關(guān)于LeanCloud的數(shù)據(jù)存儲(chǔ)使用方法可以在里面找到,本文不講述關(guān)于LeanCloud的使用,知識(shí)借助LeanCloud平臺(tái)舉一個(gè)在網(wǎng)絡(luò)上存儲(chǔ)數(shù)據(jù)的例子。

二、使用方法

1.上傳數(shù)據(jù)

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. 讀取數(shù)據(jù)

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">獲取數(shù)據(jù)</string>
 <string name="put_data">上傳數(shù)據(jù)</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類(lèi)

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

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

下載地址:http://xiazai.jb51.net/201612/yuanma/DataStorageDemo-master_jb51.rar

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Android根據(jù)包名停止其他應(yīng)用程序的方法

    Android根據(jù)包名停止其他應(yīng)用程序的方法

    這篇文章主要介紹了Android根據(jù)包名停止其他應(yīng)用程序,需要的朋友可以參考下
    2020-03-03
  • 淺談Android Studio如何Debug對(duì)應(yīng)so文件C/C++代碼

    淺談Android Studio如何Debug對(duì)應(yīng)so文件C/C++代碼

    本篇文章主要介紹了淺談Android Studio如何Debug對(duì)應(yīng)so文件C/C++代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • android中Invalidate和postInvalidate的更新view區(qū)別

    android中Invalidate和postInvalidate的更新view區(qū)別

    Android中實(shí)現(xiàn)view的更新有兩組方法,一組是invalidate,另一組是postInvalidate,其中前者是在UI線程自身中使用,而后者在非UI線程中使用,感興趣的朋友可以了解下哦
    2013-01-01
  • Kotlin函數(shù)式編程超詳細(xì)介紹

    Kotlin函數(shù)式編程超詳細(xì)介紹

    一個(gè)函數(shù)式應(yīng)用通常由三大類(lèi)函數(shù)構(gòu)成:變換transform、過(guò)濾filters合并combineo每類(lèi)函數(shù)都針對(duì)集合數(shù)據(jù)類(lèi)型設(shè)計(jì),目標(biāo)是產(chǎn)生一個(gè)最終結(jié)果。函數(shù)式編程用到的函數(shù)生來(lái)都是可組合的,也就是說(shuō),你可以組合多個(gè)簡(jiǎn)單函數(shù)來(lái)構(gòu)建復(fù)雜的計(jì)算行為
    2022-09-09
  • Android自定義水平進(jìn)度條的圓角進(jìn)度

    Android自定義水平進(jìn)度條的圓角進(jìn)度

    這篇文章主要為大家詳細(xì)介紹了Android自定義水平進(jìn)度條的圓角進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié)

    淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié)

    本篇文章主要介紹了淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Android JetpackCompose使用教程講解

    Android JetpackCompose使用教程講解

    在今年的Google/IO大會(huì)上,亮相了一個(gè)全新的 Android 原生 UI 開(kāi)發(fā)框架-Jetpack Compose, 與蘋(píng)果的SwiftIUI一樣,Jetpack Compose是一個(gè)聲明式的UI框架
    2022-10-10
  • Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)

    Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)

    這篇文章主要為大家介紹了Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Kotlin定義其他類(lèi)的實(shí)現(xiàn)詳解

    Kotlin定義其他類(lèi)的實(shí)現(xiàn)詳解

    這篇文章主要介紹了Kotlin定義其他類(lèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Android ConstraintLayout約束布局使用詳解

    Android ConstraintLayout約束布局使用詳解

    ConstraintLayout 即約束布局,也是 Android Studio 的默認(rèn)布局,它可以減少布局的層級(jí),改善布局性能。不夸張地說(shuō),它基本上可以實(shí)現(xiàn)任何你想要的布局效果,下面,咱們一起來(lái)瞧瞧吧
    2022-11-11

最新評(píng)論