【移動應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android之網(wǎng)絡(luò)數(shù)據(jù)存儲_第4頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論