/**
*
*/
package com.sky_dreaming.weather.util;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.util.TimeFormatException;
import com.sky_dreaming.weather.entity.ForecastEntity;
import com.sky_dreaming.weather.entity.WidgetEntity;
import com.sky_dreaming.weather.fusion.FusionField;
/**
* ****************************************************************
* 文件名称 : WebServiceHelper.java
* 作 者 : sky_dreaming
* 创建时间 : 2010-10-26 下午04:23:10
* 文件描述 : 提供联网获取数据方法,数据库获取更新数据并封装实体方法
*****************************************************************
*/
public class WebServiceUtil {
private static final String TAG = "WebServiceHelper";
/**
* 只在本文件中用到的静态字符串
*/
private static final String FORECAST_INFORMATION = "forecast_information";
private static final String CURRENT_CONDITIONS = "current_conditions";
private static final String FORECAST_CONDITIONS = "forecast_conditions";
private static final String PROBLEM_CAUSE = "problem_cause";
private static final String CITY = "city";
private static final String FORECAST_DATE = "forecast_date";
private static final String CONDITION = "condition";
private static final String TEMP_F = "temp_f";
private static final String TEMP_C = "temp_c";
private static final String HUMIDITY = "humidity";
private static final String ICON = "icon";
private static final String WIND_CONDITION = "wind_condition";
private static final String DAY_OF_WEEK = "day_of_week";
private static final String LOW = "low";
private static final String HIGH = "high";
/**
* 更新天气预报信息
* @param context
* @param widgetUri
* @throws ForecastParseException
*/
public static void updateForecasts(Context context, Uri widgetUri)
throws ForecastParseException {
Log.d(TAG, "update the nowest forecast!");
/**
* forecastUri:"content://com.ty.weather/widgets/" + widgetId + "/forecasts"
*/
Uri forecastUri = Uri.withAppendedPath(widgetUri,
FusionField.FORECAST_END);
/**
* 获取ContentResolver对象
*/
ContentResolver resolver = context.getContentResolver();
/**
* 从widgetUri获取邮编
*/
Cursor cursor = null;
String postalCode = null;
try {
cursor = resolver.query(widgetUri,
new String[] {FusionField.POSTALCODE }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
postalCode = cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
/**
* 获取天气信息并封装实体类数据对象
*/
WidgetEntity widgetEntity = queryWebservice(postalCode);
if (widgetEntity == null) {
throw new ForecastParseException(
"No forecasts found from webservice query");
}
/**
* 清空原来"/forecasts"信息,数据项对应于天气预报详细信息界面显示
*/
resolver.delete(forecastUri, null, null);
/**
* 重新填入"/forecasts"信息,数据项对应于天气预报详细信息界面显示
*/
ContentValues values = new ContentValues();
for (ForecastEntity forecast : widgetEntity.getDetails()) {
values.clear();
values.put(FusionField.DAYOFWEEK, forecast.getDayOfWeek());
values.put(FusionField.HIGHT, forecast.getHight());
values.put(FusionField.LOW, forecast.getLow());
values.put(FusionField.ICON, forecast.getIcon());
values.put(FusionField.CONDITION, forecast.getCondition());
Log.d(TAG, "insert detail forecast infomations:"
+ forecast.getDayOfWeek());
resolver.insert(forecastUri, values);
}
/**
* 更新Widget界面信息,数据项对应于Widget界面显示
*/
values.clear();
values.put(FusionField.CITY, widgetEntity.getCity());
values.put(FusionField.CONDITION, widgetEntity.getCondition());
values.put(FusionField.FORECASTDATE, widgetEntity.getForecastDate());
values.put(FusionField.HUMIDITY, widgetEntity.getHumidity());
values.put(FusionField.ICON, widgetEntity.getIcon());
values.put(FusionField.TEMPC, widgetEntity.getTempC());
values.put(FusionField.TEMPF, widgetEntity.getTempF());
values.put(FusionField.WINDCONDITION, widgetEntity.getWindCondition());
values.put(FusionField.LAST_UPDATE_TIME, System.currentTimeMillis());
Log.d(TAG, "update the weather infomation");
resolver.update(widgetUri, values, null, null);
}
/**
* 通过邮编获取某个地区的天气信息,并封装成实体类数据对象
* @param postalCode
* @return
*/
private static WidgetEntity queryWebservice(String postalCode)
throws ForecastParseException{
if (postalCode == null) {
throw new ForecastParseException("can not covert to entity");
}
/**
* 联网获取数据的URL
*/
String httpURL = String.format(FusionField.WEBSERVICE_URL, postalCode);
Log.d(TAG, "uri:" + httpURL);
Reader responseReader;
WidgetEntity widgetEntity = null;
/**
* HttpClient
*/
HttpClient client = new DefaultHttpClient();
/**
* HttpGet方式提交参数获取数据
*/
HttpGet request = new HttpGet(httpURL);
try {
Log.d(TAG, "get google's weather infomation");
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
Log.d(TAG, "Request returned status " + status);
if(status.getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
responseReader = new InputStreamReader(entity.getContent(), "GB2312");
/**
* 解析数据流
*/
if (responseReader != null) {
widgetEntity = parseResponse(responseReader);
}
}
} catch (UnsupportedEncodingException e) {
throw new ForecastParseException("Problem calling forecast API", e);
} catch (IllegalStateException e) {
throw new ForecastParseException("Problem calling forecast API", e);
} catch (IOException e) {
throw new ForecastParseException("Problem calling forecast API", e);
}
return widgetEntity;
}
/**
* xml转换为实体
*
* @param responseReader
* @return
*/
private static WidgetEntity parseResponse(Reader responseReader)
throws ForecastParseException {
Log.d(TAG, "conver xml to widgetEntity");
WidgetEntity widgetEntity = new WidgetEntity();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
String tagName = null;
xpp.setInput(responseReader);
int eventType = xpp.getEventType();
/**
* SAX方式循环解析xml文件
*/
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
/**
* 获取xml元素名
*/
tagName = xpp.getName();
/**
* 城市代码错误,抛出异常
*/
if (PROBLEM_CAUSE.equals(tagName)) {
throw new ForecastParseException("the city is non correct!");
} else if (FORECAST_INFORMATION.equals(tagName)) {
/**
* forecast_information
*/
dealWithInfomation(
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android-天气预报(源码).zip (122个子文件)
resources.ap_ 1.22MB
WeatherWidget2.1.apk 1.24MB
WebServiceUtil.class 12KB
ForecastService.class 6KB
ForecastProvider.class 6KB
DetailForecastActivity.class 6KB
ForecastWidget.class 5KB
ConfigureActivity.class 4KB
ForecastUtil.class 4KB
WidgetEntity.class 4KB
R$drawable.class 3KB
ForecastTimeService.class 2KB
DetailForecastActivity$ForecastAdapter.class 2KB
FusionField.class 2KB
DBHelper.class 2KB
ForecastEntity.class 2KB
R$id.class 1KB
R$string.class 902B
WebServiceUtil$ForecastParseException.class 794B
R.class 729B
R$anim.class 702B
R$color.class 562B
R$layout.class 541B
R$xml.class 412B
R$attr.class 358B
.classpath 280B
classes.dex 42KB
WebServiceUtil.java 17KB
ForecastProvider.java 9KB
DetailForecastActivity.java 8KB
ForecastUtil.java 8KB
R.java 8KB
ForecastService.java 8KB
ForecastWidget.java 7KB
ConfigureActivity.java 5KB
WidgetEntity.java 4KB
FusionField.java 3KB
ForecastTimeService.java 2KB
DBHelper.java 2KB
ForecastEntity.java 2KB
layer_lightning1.png 63KB
layer_lightning2.png 60KB
cloudy.png 58KB
storm.png 56KB
layer_lightning3.png 55KB
rain.png 51KB
lightrain.png 51KB
weather_lightrain.png 51KB
snow.png 42KB
icerain.png 41KB
sun.png 37KB
layer_cloud2.png 31KB
layer_cloud1.png 29KB
weather_chancestorm.png 28KB
weather_chancesnow_n.png 28KB
weather_icyrain.png 28KB
weather_chancestorm_n.png 28KB
weather_cloudyrain.png 27KB
cloudyrain.png 27KB
weather_storm.png 26KB
weather_mostlycloudy_n.png 26KB
weather_chancesnow.png 25KB
weather_cloudyrain_n.png 25KB
weather_rain.png 25KB
weather_mostlycloudy.png 23KB
weather_sunny.png 23KB
flurries.png 22KB
weather_mostlysunny.png 22KB
mostlysunny.png 22KB
weather_snow.png 22KB
weather_haze.png 21KB
weather_mostlysunny_n.png 20KB
weather_cloudy.png 16KB
weather_fog.png 15KB
fog.png 15KB
weather_flurries.png 15KB
weather_sunny_n.png 13KB
number_8_tahoma.png 5KB
weather.png 5KB
number_9_tahoma.png 5KB
number_6_tahoma.png 5KB
number_3_tahoma.png 4KB
number_0_tahoma.png 4KB
number_2_tahoma.png 3KB
number_5_tahoma.png 3KB
number_4_tahoma.png 3KB
number_7_tahoma.png 3KB
weatherbg.png 3KB
layer_drop6.png 2KB
number_1_tahoma.png 2KB
layer_drop5.png 2KB
layer_drop1.png 2KB
layer_drop7.png 2KB
dots.png 1KB
layer_drop4.png 1KB
layer_drop3.png 958B
layer_drop2.png 838B
rain1.png 195B
rain3.png 189B
rain2.png 184B
共 122 条
- 1
- 2
资源评论
firepation
- 粉丝: 1973
- 资源: 1302
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功