



下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
一、網(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)址:/。關(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,
);
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 護(hù)理操作技術(shù)風(fēng)險(xiǎn)與防范考核試題及答案
- 流動(dòng)相安全交底模板
- 2025年神農(nóng)架林區(qū)社區(qū)專職工作者招聘考試筆試試題(含答案)
- 2025年華潤(rùn)電力控股內(nèi)蒙古區(qū)域招聘考試筆試試題(含答案)
- 老板的稅務(wù)基礎(chǔ)課件
- 老年友善管理課件
- 2025年安裝維修行業(yè)調(diào)研分析及前景預(yù)測(cè)報(bào)告
- 場(chǎng)項(xiàng)目投標(biāo)失敗后的品牌形象重塑與宣傳推廣合同
- 體育場(chǎng)館租賃合同范本365版
- 北京市住建委房屋買賣合同
- 2025春季學(xué)期國(guó)開電大??啤豆芾韺W(xué)基礎(chǔ)》期末紙質(zhì)考試總題庫(kù)
- 物流倉(cāng)儲(chǔ)設(shè)備選型與配置規(guī)范
- T-BSRS 124-2024 伴生放射性礦開發(fā)利用場(chǎng)地土壤放射性污染調(diào)查和修復(fù)監(jiān)測(cè)技術(shù)規(guī)范
- (2025)全國(guó)交管12123學(xué)法減分考試題庫(kù)附答案
- 虛擬現(xiàn)實(shí)行業(yè)標(biāo)準(zhǔn)-深度研究
- T-ZHCA 025-2023 化妝品抗氧化人體測(cè)試方法
- 安保主管上半年工作總結(jié)
- 中山市招商服務(wù)中心2025年上半年招考人員易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2022年9月國(guó)家開放大學(xué)??啤陡叩葦?shù)學(xué)基礎(chǔ)》期末紙質(zhì)考試試題及答案
- 包皮環(huán)切術(shù)的健康宣教
- 葡萄收購(gòu)合同范例
評(píng)論
0/150
提交評(píng)論