/**
*
*/
package com.ty.weather;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
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.ty.weather.ForecastProvider.WeatherWidgets;
import com.ty.weather.util.ForecastEntity;
import com.ty.weather.util.WidgetEntity;
/**
* @author 088926
*
*/
public class WebServiceHelper {
private static final String TAG = "WebServiceHelper";
private static final String WEBSERVICE_URL = "http://www.google.com/ig/api?weather=%s&hl=zh-cn";
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";
public static void updateForecasts(Context context, Uri widgetUri)
throws ForecastParseException {
Log.d(TAG, "update the nowest forecast!");
Uri forecastUri = Uri.withAppendedPath(widgetUri,
WeatherWidgets.FORECAST_END);
ContentResolver resolver = context.getContentResolver();
Cursor cursor = null;
String postalCode = null;
try {
cursor = resolver.query(widgetUri,
new String[] { WidgetEntity.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");
}
resolver.delete(forecastUri, null, null);
ContentValues values = new ContentValues();
for (ForecastEntity forecast : widgetEntity.getDetails()) {
values.clear();
values.put(ForecastEntity.DAYOFWEEK, forecast.getDayOfWeek());
values.put(ForecastEntity.HIGHT, forecast.getHight());
values.put(ForecastEntity.LOW, forecast.getLow());
values.put(ForecastEntity.ICON, forecast.getIcon());
values.put(ForecastEntity.CONDITION, forecast.getCondition());
Log.d(TAG, "insert detail forecast infomations:"
+ forecast.getDayOfWeek());
resolver.insert(forecastUri, values);
}
values.clear();
values.put(WidgetEntity.CITY, widgetEntity.getCity());
values.put(WidgetEntity.CONDITION, widgetEntity.getCondition());
values.put(WidgetEntity.FORECASTDATE, widgetEntity.getForecastDate());
values.put(WidgetEntity.HUMIDITY, widgetEntity.getHumidity());
values.put(WidgetEntity.ICON, widgetEntity.getIcon());
values.put(WidgetEntity.TEMPC, widgetEntity.getTempC());
values.put(WidgetEntity.TEMPF, widgetEntity.getTempF());
values.put(WidgetEntity.WINDCONDITION, widgetEntity.getWindCondition());
values.put(WidgetEntity.LAST_UPDATE_TIME, System.currentTimeMillis());
Log.d(TAG, "update the weather infomation");
resolver.update(widgetUri, values, null, null);
}
public static WidgetEntity queryWebservice(String postalCode)
throws ForecastParseException {
if (postalCode == null) {
throw new ForecastParseException("can not covert to entity");
}
Log.d(TAG, "uri:" + String.format(WEBSERVICE_URL, postalCode));
Reader responseReader;
WidgetEntity widgetEntity = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(String.format(WEBSERVICE_URL, postalCode));
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);
HttpEntity entity = response.getEntity();
responseReader = new InputStreamReader(entity.getContent(), "GB2312");
} catch (IOException e) {
throw new ForecastParseException("Problem calling forecast API", e);
}
// If response found, send through to parser
if (responseReader != null) {
widgetEntity = parseResponse(responseReader);
}
return widgetEntity;
}
/**
* 转换为实体
*
* @param responseReader
* @return
*/
private static WidgetEntity parseResponse(Reader responseReader)
throws ForecastParseException {
// TODO Auto-generated method stub
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();
Log.d(TAG, "eventType:" + eventType);
while (eventType != XmlPullParser.END_DOCUMENT) {
//Log.d(TAG, "eventType:" + eventType);
if (eventType == XmlPullParser.START_TAG) {
tagName = xpp.getName();
Log.d(TAG, "tag Name:" + tagName);
if (PROBLEM_CAUSE.equals(tagName)) {
throw new ForecastParseException(
"the city is non correct!");
} else if (FORECAST_INFORMATION.equals(tagName)) {
dealWithInfomation(tagName, widgetEntity, xpp);
} else if (CURRENT_CONDITIONS.equals(tagName)) {
dealWithCurrentConditions(tagName, widgetEntity, xpp);
} else if (FORECAST_CONDITIONS.equals(tagName)) {
dealWithForecastConditions(tagName, widgetEntity, xpp);
}
}
eventType = xpp.next();
}
} catch (IOException e) {
throw new ForecastParseException("Problem parsing XML forecast", e);
} catch (XmlPullParserException e) {
throw new ForecastParseException("Problem parsing XML forecast", e);
} catch (TimeFormatException e) {
throw new ForecastParseException("Problem parsing XML forecast", e);
}
Log.d(TAG, "covert xml to entity success");
return widgetEntity;
}
private static void dealWithForecastConditions(String name,
WidgetEntity widgetEntity, XmlPullParser xpp) throws IOException,
XmlPullParserException {
// TODO Auto-generated method stub
Log.d(TAG, "dealWithForecastConditions");
ForecastEntity forecast = new ForecastEntity();
xpp.next();
int eventType = xpp.getEventType();
String tagName = xpp.getName();
while ((!name.equals(tagName) || eventType != XmlPullParser.END_TAG)
&& eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (tagName.equals(CONDITION)) {
forecast.setCondition(xpp.getAttributeValue(null, "data"));
} else if (tagName.equals(DAY_OF_WEEK)) {
forecast.setDayOfWeek(xpp.getAttributeValue(null, "data"));
} else if (tagName.equals(HIGH)) {
forecast.setHight(Integer.parseInt(xpp.getAttributeValue(
没有合适的资源?快使用搜索试试~ 我知道了~
Android程序研发源码Android UI界面漂亮的天气预报源码.zip
共118个文件
png:60个
class:24个
xml:16个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 131 浏览量
2023-04-30
17:40:13
上传
评论
收藏 3.83MB ZIP 举报
温馨提示
Android程序研发源码Android UI界面漂亮的天气预报源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
Android程序研发源码Android UI界面漂亮的天气预报源码.zip (118个子文件)
resources.ap_ 1.22MB
ty-weather.apk 1.24MB
WebServiceHelper.class 11KB
ForecastProvider.class 6KB
ForecastService.class 6KB
DetailForecastActivity.class 6KB
ForecastWidget.class 4KB
WidgetEntity.class 4KB
ConfigureActivity.class 4KB
ForecastUtil.class 4KB
R$drawable.class 2KB
DetailForecastActivity$ForecastAdapter.class 2KB
ForecastEntity.class 2KB
ForecastTimeService.class 2KB
ForecastProvider$DatabaseHelper.class 2KB
R$id.class 1KB
ForecastProvider$WeatherWidgets.class 915B
ForecastProvider$WeatherDetails.class 876B
R$string.class 872B
WebServiceHelper$ForecastParseException.class 757B
R$anim.class 672B
R.class 629B
R$color.class 532B
R$layout.class 483B
R$xml.class 382B
R$attr.class 328B
.classpath 280B
Thumbs.db 140KB
classes.dex 42KB
WebServiceHelper.java 13KB
ForecastProvider.java 11KB
R.java 8KB
DetailForecastActivity.java 7KB
ForecastUtil.java 7KB
ForecastService.java 5KB
WidgetEntity.java 4KB
ConfigureActivity.java 4KB
ForecastWidget.java 4KB
ForecastEntity.java 2KB
ForecastTimeService.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
共 118 条
- 1
- 2
资源评论
HappyGirl快乐女孩
- 粉丝: 1w+
- 资源: 4160
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功