安卓数据存储_第1页
安卓数据存储_第2页
安卓数据存储_第3页
安卓数据存储_第4页
安卓数据存储_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

电子与信息「•程学院实验报告

Android编程技术实验卡片

实验编号:G280120503实验名称及学时:数据存储

姓名:杨旋班级:15级计本1班

学号:2022024027实验室:A304

实验情况评价(教师填写):

避镂赢(教师填写):

参照如下页面设计效果,设计一个App/用界面,完成数据存储及解析的相关操作。要

求:

(1)要为登录界而添加记住我的复选框,当复选框选中时,使用SharedPreferences

存储类在合适的时机实现登录名和密码的保存,F次登录时直接登录。

(2)登录成功后显示多个城市的天气预报效果。天气信息使用XML文档和JSON文档两

种方式实现,并在程序中解析使用。

Lo^^UrtActwity

(3)设计程序界面,添加两个按钮,单击按钮1将List"eather>list;序列化为XML

文档。单击按钮2将List<weather>lisl;生成JSON数据文件。以当前的weather数据模

型为例。

2.预习内容

LI界面设计,Intent对象使用,SharedPreferences存储类的使用过程,XML文件的序

列化及PULL解析,JSON对象,JSON数组的定义和使用,JSO\文件的生成和解析。

实验报告1开辟环境搭建测试

3.实验类型

验证型

4.实验目的

(1)掌握Intent启动Activity。

(2)掌握SharedPrefererces存储数据流程。

(3)掌握XML和JSON数据文件的序列化和解析(PULL和Gson)。

5.实验要求

(1)打开AS环境,创建Android项目使用布局文档XML完成UI界面的设计,按照实验

内容要求,完成Activity的逻辑事件处理。

(2)完成实验内容要求并进行测试,分析结果。

(3)整理自己对XML和Json文档的生成和解析的相关类,作为工具类留用。

6.实验过程(源文件、步骤及结果分析)

packageCheckBoxcheckBox;

com.example.android.saveqq;Buttonbutton;

importandroid.content.Context;0Override

importandroid.content.Intent;protectedvoidonStart(){

importsuper.onStart();

android,content.SharedPreferenSharedPreferencessp=

ces;getApplicationContext().getSha

importredPreferences("data",

android.support.v7.app.AppCompContPXt.MODE_PRIVATE);

atActivity;Stringusername=

importandroid.os.Bundle;sp.getStr=ng("name”,null);

importandroid.viev;.View;Stringpass=

importandroid.widget.Button;sp.getStrmg("pwd",null);

importandroid.widget.CheckBox;name.setText(username);

importandroid.widget.EditText;password.setText(pass);

if(username!=null&&

importstaticandroid.R.id.edit;pass!=null){

checkBox.setChecked(true);

publicclass)

LoginActivityextends)

AppCompatActivity{

©Override

EditTextname,password;protectedvoidonCreate(Bundle

由.子与信息丁理学除实盼相告

savedlnstanceState){)

super.onCreate(savedinstanceSt

ate);@Override

setContentView(R.layout.activiprotectedvoidonStop(){

ty_login);super.onStop();

name=(EditText)SharedPreferencessp=

findViewByld(R.id.et_name);getApplicationcontext().getSha

password=(EditText)redPreferences("data”,

findViewByld(R.id.et_password);Context.MODE_PRIVATE);

checkBox=(CheckBox)SharedPreferences.Editoredit=

findviewByid(K.id.re_check);sp.edit();

button=(Button)if(checkBox.isChecked())(

findViewByld(R.id.btn_login);

button.setOnClickListener(newedit.putString("name",

View.OnClickListener(){name.getText().toString());

©Overrideedit.putString("pwd",

publicvoidonClick(Viewv){password.getText().toString());

Intensintent=e}lse{

newIntent(LoginActivity.this,edit.clear();

MainActivity.class);}

startActivity(intent);edit.commit();

))

));

packageimportandroid.os.Bundle;

com.example.android.weatherl;importandroid.view.View;

import

importandroid.view.View.OnClickListe

android.support.v7.app.AppCompner;

atActivity;import

实验报告1开辟环境搭建测试

android.rigpt.TinagAV-AW;t.his.gprRpsonrcAS().opAnRawRAS

importandroid.widget.Textview;ource(R.raw.weather2);

importandroid.widget.Toast;

List<Weatherlnfo>weatherInfos

importjava.io.Inputstream;

importjava.util.ArrayList;WeatherService.getlnfosFromJso

importjava.util.HashMap;n(is);

importjava.util.List;

importjava.util.Map;list=newArrayList<Map<String,

String>>();

publicclassfor(Weatherinfoinfo:

MainActivityextendsweatherinfos){

AppCompatActivityimplementsmap=newHashMap<String,

Viev/.OnClickListener(String>();

map.put(ntemp**,

privateTextVievztvCity;info.getTemp());

privateTextViewtvWeather;map.put(Hweather",

privateTextVievztvTemp;info.getWeather());

privateTextVievztvWind;map.put(nnamen,

privateTextVievztvPm;info.getName());

privateImageViewivIcon;map.put("pmn,info.getPmO);

map.put("wind",

privateMap<StringzString>map;

privateList<Map<String,info.getWinci());

String>>list;list.add(nap);

privateStringtemp,weather,}

name,pm,wind;}catch(Exceptione){

@Overridee.printStackTrace();

protectedvoidonCreate(BundleToast.makeText(this,”解析信息失

savedlnstanceState){败”,Toast.LENGTH_SHORT).show();

super.onCreate(savedinstanceSt)

ate);

setContentView(R.layout.activigetMap(1zR.drawable.sun);

ty_main);)

initView();privatevoidinitView(){

try(tvCity=(Textview)

//Inputstreamis=findViev/Byld(R.id.tv_city);

this.getResources().openRawRestvWeather=(Textview)

ource(R.raw.weatherl);findViewByld(R.id.tv_weather);

//tvTemp=(Textview)

List<Weatherlnto>weatnerlntosfindViev/Byld(R.id.tv_temp);

tvWind=(Textview)

WeatherService.getlnfosFromXMLfindViewByld(R.id.tv_wind);

(is);tvPm=(Textview)

Inputstreamis=findViewByld(R.id.tv_pm);

电子与信息「•程学院实验报告

ivlcon=tvPm.sptT«xt("pin:"+pm);

(Imageview)findViewByEd(R.id.iivlcon.seLlmageResource(iconNu

v_icon);mber);

findViewByld(R.id.btn_bj).setO)

nClickListener(this);)

findViewByld(R.id.btn_sh).setO

nClickListener(this);package

findViewByld(R.id.btn_gz).setOcom.example.android.weatherl;

nClickListener(this);

)impoxtciudLQid.uLil.Xml;

©Overrideimportcon.google.gson.Gson;

publicvoidonClick(Viewv){import

com.google.gson.reflect.TypeTo

switch(v.getld()){ken;

caseR.id.btn_sh:

getMap(0zimport

R.drawable.cloud_sun);org.xmlpuLl.vl.XmlPullParser;

break;importjava.io.lOException;

caseR.id.btn_bj:importjava.io.InputStream;

getMap(1,R.drawable.sun);importjava.lang.reflect.Type;

break;importjava.util.ArrayList;

caseR.id.btn_gz:importjava.util.List;

getMap(2,R.drawable.clouds);

break;

)publicclassWeatherService{

)publicstatic

List<WeatherInfo>getlnfosFromX

privatevoidgetMap(intnumber,ML(Inputstreamis)throws

inticonNumber){Exceptionj

Map<String,XmlPullParserparser=

String>cityMap=Xml.newPullParser();

list.get(number);parser.setinput(is,nutf-8n);

temp=cityMap.get("temp**);

weather=List<WeatherInfoweatherinfos

cityMap.get('•weather*');=null;

name=cityMap.get(Mnamen);WeatherInfoweatherInfo=null;

pm=cityMap.get(npmn);inttype=

wind=cityMap.get(nwind,f);parser.getEventType();

tvCity.setText(name);

tvWeather.setText(weather);while(type!=

tvTemp.setText("n+temp);XmlPullParser.END_DOCUMENT){

tvWind.setText(+wind);switch(type){

实验报告1开辟环境搭建测试

caseXmlPullParser.START_TAG:eO)){

if("infos'*.equals(parser.getNaString

me()))(wind=parser.nextText();

weatherinfos=newweatherinfo.setwind(wind);

ArrayList<WeatherInfo>();}

els}ebreak;

if("city”.equals(parser.getNamcaseXmlPullParser.END_TAG:

e()))(if("city".equals(parser.getNam

weatherinfo=newWeatherinfo();e())){

Stringweatherinfos.add(weatherlnfo);

idStr=weatherinfo=null;

parser.getAttributevalue(0);}

weatherInfo.setId(idStr);break;

els}e|

if("temp".equals(parser.getNamtyp=eparser.next();

e()))()

String

temp=parser.nextText();returnweatherinfos;

weatherinfo.setTemp(temp);)

els}e

if(**weathern.equals(parser.getpublicstatic

Name()))(List<Weatherlnfo>getlnfosFromJ

Stringson(Inputstreamis)

weather=parser.nextText();throwsIOException{

weatherinfo.setWeather(weatherbyte[]buffer=new

);byte[is.available()];

els}eis.read(buffer);

if{'*name*'.equals(parser.getNamStringjsonnewString

e())){(buffer,Mutf-8");

StringGsongson=newGson();

name=parser.nextText();TypeListType=new

weatherlnfo.setName(name);TypeToken<List<WeatherInfo>>()

els}e{}.getType();

if(npmn.equals(parser.getName(

))){List<Weatherlnfo>weatherlnfos

Strpiirmq=gson.fromJson(json,listType)

parser.nextText();returnweatherinfos;

weat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论