您的位置:首页 > 理论基础 > 计算机网络

第7讲 Android网络与数据存储

2016-06-22 17:23 501 查看

第7讲Android网络与数据存储

项目要求:做一个天气应用

a)参考接口:http://www.weather.com.cn/data/list3/city.xml

b)考察内容:获取数据,解析JSON

c)数据缓存在数据库中,使用SharedPreferences来处理,采用手动刷新数据。

7.1项目总体结构

为了规范代码,进行了如下的分包。



7.2创建数据库与表

db包下文件分别是创建数据库和对数据库的操作:数据库结构如下图



省份表



选中的城市表



选中的区县表



对数据库常用操作封装类结构如下:

/**

*CreatedbyAdministratoron2016/6/20.

*/

publicclassWeatherDB{

//定义数据库名

publicstaticfinalStringDB_NAME="weather";

//定义数据库版本号

publicstaticfinalintVERSION=1;

//定义类对象

privatestaticWeatherDBsWeatherDB;

//定义数据库对象

privateSQLiteDatabasedb;

//私有化构造方法

privateWeatherDB(Contextcontext){

WeatherOpenHelperdbHelper=new
WeatherOpenHelper(context,DB_NAME,null,VERSION);

db=dbHelper.getReadableDatabase();

}

//实例化类对象

publicsynchronizedstatic
WeatherDBgetInstance(Contextcontext){

if(sWeatherDB==
null
){

sWeatherDB=newWeatherDB(context);

}

returnsWeatherDB;

}

//将province省份存储到数据库

publicvoidsaveprovince(Provinceprovince){

if(province!=
null
){

ContentValuesvalues=newContentValues();

values.put("province_name",province.getProvinceName());

values.put("province_code",province.getProvinceCode());

db.insert("Province",null,values);

}

}

//从数据库读取全国所有的省份信息

publicList<Province>loadProvinces(){

List<Province>list=newArrayList<Province>();

Cursorcursor=db.query("Province",null,
null,null,
null,null,
null);

while(cursor.moveToNext()){

Provinceprovince=newProvince();

province.setId(cursor.getInt(cursor.getColumnIndex("id")));

province.setProvinceName(cursor.getString(cursor.getColumnIndex(
"province_name"
)));

province.setProvinceCode(cursor.getString(cursor.getColumnIndex(
"province_code"
)));

//Log.i("FXC","从数据库读取全国所有的省份信息"+province.getProvinceName());

list.add(province);

}

if(cursor!=null){

cursor.close();

}

returnlist;

}

//将city城市存储到数据库

publicvoidsavecity(Citycity){

if(city!=null){

ContentValuesvalues=newContentValues();

values.put("city_name",city.getCityName());

values.put("city_code",city.getCityCode());

values.put("province_id",city.getProvinceId());

db.insert("City",null,values);

}

}

//从数据库读取指定省份城市信息

publicList<City>loadCities(intprovinceId){

List<City>list=newArrayList<City>();

Cursorcursor=db.query("City",null,
null,null,
null,null,
null);

while(cursor.moveToNext()){

Citycity=newCity();

city.setId(cursor.getInt(cursor.getColumnIndex("id")));

city.setCityName(cursor.getString(cursor.getColumnIndex(
"city_name"
)));

city.setCityCode(cursor.getString(cursor.getColumnIndex(
"city_code"
)));

city.setProvinceId(cursor.getInt(cursor.getColumnIndex("province_id")));

list.add(city);

}

if(cursor!=null){

cursor.close();

}

returnlist;

}

//将city城市下一级区县存储到数据库

publicvoidsavecounty(Countycounty){

if(county!=null){

ContentValuesvalues=newContentValues();

values.put("county_name",county.getcountyName());

values.put("county_code",county.getcountyCode());

values.put("city_id",county.getCityId());

db.insert("County",null,values);

}

}

//从数据库读取city城市下一级区县信息

publicList<County>loadCounties(intcityId){

List<County>list=newArrayList<County>();

Cursorcursor=db.query("County",null,
null,null,
null,null,
null);

while(cursor.moveToNext()){

Countycounty=newCounty();

county.setId(cursor.getInt(cursor.getColumnIndex("id")));

county.setcountyName(cursor.getString(cursor.getColumnIndex(
"county_name"
)));

county.setcountyCode(cursor.getString(cursor.getColumnIndex(
"county_code"
)));

county.setCityId(cursor.getInt(cursor.getColumnIndex("city_id")));

list.add(county);

}

if(cursor!=null){

cursor.close();

}

returnlist;

}

//删除城市数据

publicvoiddelete_city(){

db.delete("City",null,null);

}

//删除区县数据

publicvoiddelete_county(){

db.delete("County",null,null);

}

}

7.3创建model

在该包下对数据库的每一个表创建一个实体类,主要是写一些数据库表对应字段的常用的get或set方法

7.4服务器交互

全国所有省市区县的数据都是从服务器获取到的。解析的规则就是先按逗号分隔,再按单竖线分隔,接着将解析出来的数据设置到实体类中,然后调用数据库操作封装类中相应的方法存储到数据库相应表中。



部分实例如下:
if(!TextUtils.isEmpty(response)){
String[]allProvinces=response.split(",");
if(allProvinces!=null&&allProvinces.length>0){
for(Stringp:allProvinces){
String[]array=p.split("\\|");
Provinceprovince=newProvince();
province.setProvinceCode(array[0]);
province.setProvinceName(array[1]);
//Log.i("FXC","解析出来的省份是"+array[1]);
weatherDB.saveprovince(province);
}
returntrue;
}
}


连接服务器代码如下:

URLurl=newURL(address);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStreamin=connection.getInputStream();
BufferedReaderreader=newBufferedReader(newInputStreamReader(in));


7.5MainActivity主活动

主要是获取列表等控件,按要求去读取本地数据库,若无数据则连接服务器。以获取城市数据为例。
//查询全国所有的市,优先从数据库查询,如果没有查询到再去服务器上查询
privatevoidqueryCities(){
if(flag==true){
if(selectprovince.getProvinceName().equals(selectedprovince.getProvinceName())){

mCityList=mWeatherDB.loadCities(selectprovince.getId());
if(mCityList.size()>0){
dataList.clear();
for(Citycity:mCityList){
dataList.add(city.getCityName());
}
mAdapter.notifyDataSetChanged();
listview.setSelection(0);
titletext.setText(selectprovince.getProvinceName());
currentLevel=LEVEL_CITY;
}}}else{
if(flag==false)mWeatherDB.delete_city();//删除库中旧的数据
queryFromServer(selectprovince.getProvinceCode(),"city");//连接服务器获取新数据
flag=true;
selectedprovince=selectprovince;
}
}




7.6获取天气

{"weatherinfo":{"city":"吴中","cityid":"101190405","temp1":"19℃","temp2":"11℃","weather":"小雨","img1":"d7.gif","img2":"n7.gif","ptime":"08:00"}}

JSONObjectjsonObject=newJSONObject(response);
JSONObjectweatherInfo=jsonObject.getJSONObject("weatherinfo");
StringcityName=weatherInfo.getString("city");
StringweatherCode=weatherInfo.getString("cityid");
Stringtemp1=weatherInfo.getString("temp1");
Stringtemp2=weatherInfo.getString("temp2");
StringweatherDesp=weatherInfo.getString("weather");
StringpublishTime=weatherInfo.getString("ptime");
savaWeatherInfo(context,cityName,weatherCode,temp1,temp2,weatherDesp,publishTime);




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: