



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】Android之網(wǎng)絡數(shù)據(jù)存儲
一、網(wǎng)絡保存數(shù)據(jù)介紹可以使用網(wǎng)絡來保存數(shù)據(jù),在需要的時候從網(wǎng)絡上獲取數(shù)據(jù),進而顯示在App中。用網(wǎng)絡保存數(shù)據(jù)的方法有很多種,對于不同的網(wǎng)絡數(shù)據(jù)采用不同的上傳與獲取方法。本文利用LeanCloud來進行網(wǎng)絡數(shù)據(jù)的存儲。LeanCloud是一種簡單高效的數(shù)據(jù)和文件存儲服務。感興趣的可以查看網(wǎng)址:/。關于LeanCloud的數(shù)據(jù)存儲使用方法可以在里面找到,本文不講述關于LeanCloud的使用,知識借助LeanCloud平臺舉一個在網(wǎng)絡上存儲數(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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年安全防護欄安裝合同
- 2025年合同糾紛解決:勞動合同仲裁與調(diào)解
- 簡易閣樓施工合同范本
- 2025【工業(yè)類合同參考格式(2)】:工業(yè)類小說
- 吧臺設備采購合同范本
- 腺樣體肥大的術后護理
- 門急診護理組的述職報告
- 定制家具訂購合同范本
- 2025汽車抵押貸款的合同協(xié)議書
- 返前教職工防控知識培訓
- 防城港職業(yè)技術學院籌設實施方案
- 螺桿泵工作原理和工況診斷方法
- 城市雕塑藝術工程量清單計價定額2020版
- 醫(yī)患溝通技巧(PPT)課件
- 真理誕生于一百個問號之后(優(yōu)秀)(課堂PPT)
- 污水處理廠防汛應急演練方案
- 慢性阻塞性肺疾病(COPD)的藥物治療
- 淘汰賽賽對陣表
- 英文形式發(fā)票樣本
- 服裝質(zhì)量檢驗表最新
- 發(fā)電機臨時用電施工方案
評論
0/150
提交評論