下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲
一、網(wǎng)絡(luò)保存數(shù)據(jù)介紹可以使用網(wǎng)絡(luò)來保存數(shù)據(jù),在需要的時(shí)候從網(wǎng)絡(luò)上獲取數(shù)據(jù),進(jìn)而顯示在App中。用網(wǎng)絡(luò)保存數(shù)據(jù)的方法有很多種,對于不同的網(wǎng)絡(luò)數(shù)據(jù)采用不同的上傳與獲取方法。本文利用LeanCloud來進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的存儲。LeanCloud是一種簡單高效的數(shù)據(jù)和文件存儲服務(wù)。感興趣的可以查看網(wǎng)址:/。關(guān)于LeanCloud的數(shù)據(jù)存儲使用方法可以在里面找到,本文不講述關(guān)于LeanCloud的使用,知識借助LeanCloud平臺舉一個(gè)在網(wǎng)絡(luò)上存儲數(shù)據(jù)的例子。二、使用方法
AVObject
personObject
=
new
AVObject(TABLENAME);
personObject.put(NAME,
);
personObject.put(AGE,
person.age);
personObject.put(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><?xmlversion="1.0"encoding="utf-8"?><android.support.design.widget.CoordinatorLayout
xmlns:android="/apk/res/android"
xmlns:tools="/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,
);
personObject.put(AGE,
person.age);
personObject.put(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"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度環(huán)保節(jié)能鋼材料采購合同范本
- 二零二四年明星肖像使用費(fèi)支付合同書3篇
- 2025年度個(gè)人旅游行程委托合同樣本4篇
- 二零二四年度智能化租賃擔(dān)保合同范本獲取3篇
- 2025版智慧農(nóng)業(yè)園區(qū)物業(yè)管理承包經(jīng)營協(xié)議3篇
- 2025年度船舶光租租賃與船舶租賃風(fēng)險(xiǎn)評估合同3篇
- 2025年度環(huán)保型打印紙大宗采購合同4篇
- 二零二五年度水電工程智能化控制系統(tǒng)安裝合同范本4篇
- 2025年度大型展覽館拆除與布展服務(wù)合同4篇
- 2025年度智能安防監(jiān)控系統(tǒng)設(shè)計(jì)與實(shí)施合同6篇
- 廣東省茂名市電白區(qū)2024-2025學(xué)年七年級上學(xué)期期末質(zhì)量監(jiān)測生物學(xué)試卷(含答案)
- 2024版?zhèn)€人私有房屋購買合同
- 2024爆炸物運(yùn)輸安全保障協(xié)議版B版
- 2025年度軍人軍事秘密保護(hù)保密協(xié)議與信息安全風(fēng)險(xiǎn)評估合同3篇
- 《食品與食品》課件
- 讀書分享會《白夜行》
- 光伏工程施工組織設(shè)計(jì)
- DB4101-T 121-2024 類家庭社會工作服務(wù)規(guī)范
- 化學(xué)纖維的鑒別與測試方法考核試卷
- 2024-2025學(xué)年全國中學(xué)生天文知識競賽考試題庫(含答案)
- 自動駕駛汽車道路交通安全性探討研究論文
評論
0/150
提交評論