amberweather数据对外提供

1、获取天气信息:
将amberweather的数据库uri传入ContentResolver的query方法得到Cursor实例。
就可以通过Cursor获取current_table中的数据,示例代码如下:

ContentResolver contentResolver;
String URI = “content://mobi.infolife.ezweather.provider.lite.WeatherProvider”;
String CURRENT_TABLE = “current_table”;
Uri uri = Uri.parse(URI + File.separator + CURRENT_TABLE);
contentResolver = context.getContentResolver();
Cursor c = contentResolver.query(uri, null, null, null, null);
if (c == null) {
Log.d(“amberweather”, “Cursor null”);
//获取失败amberweather尚未成功请求数据
} else {
if (c.getCount() != 0) {
c.moveToFirst();
//以下log依次输出为:当前温度、天气情况描述、当天最高温度、当天最低温度
Log.d(“amberweather”, “temp:” + c.getString(c.getColumnIndex(“temp”)));
Log.d(“amberweather”, “condition:” + c.getString(c.getColumnIndex(“condition”)));
Log.d(“amberweather”, “high_temp” + c.getString(c.getColumnIndex(“high_temp”)));
Log.d(“amberweather”, “low_temp” + c.getString(c.getColumnIndex(“low_temp”)));
}else {
//数据为空amberweather尚未成功请求数据
Log.d(“amberweather”, “no data”);
}
c.close();
}

 
2、获取温度单位
温度单位获取方式同上只需要改动table name 以及所需获取的字段名称
温度单位所在的数据表为:config_table,示例代码如下:

ContentResolver contentResolver;
String URI = “content://mobi.infolife.ezweather.provider.lite.WeatherProvider”;
String CONFIG_TABLE = “config_table”;
Uri uri = Uri.parse(URI + File.separator + CONFIG_TABLE);
contentResolver = getContentResolver();
Cursor c = contentResolver.query(uri, null, null, null, null);
if (c == null) {
Log.d(“amberweather”, “Cursor null”);
//获取失败amberweather尚未成功请求数据
} else {
if (c.getCount() != 0) {
c.moveToFirst();
//以下log依次输出为:温度单位id(值为int类型 0为摄氏度,1为华氏度)、温度单位名称
Log.d(“amberweather”, “temp_unit:” + c.getInt(c.getColumnIndex(“temp_unit”)));
Log.d(“amberweather”, “temp_unit_name:” + c.getString(c.getColumnIndex(“temp_unit_name”)));
}else {
//数据为空amberweather尚未成功请求数据
Log.d(“amberweather”, “no data”);
}
c.close();
}

 

 

3、获取城市信息
温度单位获取方式同上只需要改动table name 以及所需获取的字段名称
温度单位所在的数据表为:location_table,示例代码如下:
ContentResolver contentResolver;
String URI = “content://mobi.infolife.ezweather.provider.lite.WeatherProvider”;
String LOCATION_TABLE = “location_table”;
Uri uri = Uri.parse(URI + File.separator + LOCATION_TABLE);
contentResolver = getContentResolver();
Cursor c = contentResolver.query(uri, null, null, null, null);
if (c == null) {
Log.d(“amberweather”, “Cursor null”);
//获取失败amberweather尚未成功请求数据
} else {
if (c.getCount() != 0) {
c.moveToFirst();
//以下log依次输出为:level1:国家名称;level2:州府省份;level3:城市名称;lat,lon,:经纬度
Log.d(“amberweather”, “level1:” + c.getString(c.getColumnIndex(“level1”)));
Log.d(“amberweather”, “level2:” + c.getString(c.getColumnIndex(“level2”)));
Log.d(“amberweather”, “level3:” + c.getString(c.getColumnIndex(“level3”)));
Log.d(“amberweather”, “lat:” + c.getString(c.getColumnIndex(“lat”)));
Log.d(“amberweather”, “lon:” + c.getString(c.getColumnIndex(“lon”)));
}else {
//数据为空amberweather尚未成功请求数据
Log.d(“amberweather”, “no data”);
}
c.close();
}

(110)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据