可以使用網(wǎng)絡(luò)來(lái)保存數(shù)據(jù),在需要的時(shí)候從網(wǎng)絡(luò)上獲取數(shù)據(jù),進(jìn)而顯示在App中。
創(chuàng)新互聯(lián)公司致力于網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,成都網(wǎng)站設(shè)計(jì),集團(tuán)網(wǎng)站建設(shè)等服務(wù)標(biāo)準(zhǔn)化,推過(guò)標(biāo)準(zhǔn)化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進(jìn)行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場(chǎng)競(jìng)爭(zhēng)中脫穎而出。 選擇創(chuàng)新互聯(lián)公司,就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!
用網(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ù)的例子。

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

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);
}
}
});
<string name="network">Network</string> <string name="get_data">獲取數(shù)據(jù)</string> <string name="put_data">上傳數(shù)據(jù)</string>

<?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>


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);
}
}
});
}
}
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

android.net.* MainActivity AppCompatActivity "MainActivity", "onCreate", "yMNUazdBt872mNtC9aSakjYy-gzGzoHsz", "d4vw3VYdMCjLpsXRhHTBRutC"= === = Person("xiao", 23, "women"= Person("zhao", 24, "men""MainActivity", "default"
網(wǎng)頁(yè)題目:Android之網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
瀏覽路徑:http://chinadenli.net/article2/gogdoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)