㈠ android怎样使用天气预报接口 实现天气预报
使用搜索引擎可以得到很多的天气预报接口,这里以某个api为例
http://wthrcdn.etouch.cn/WeatherApi
参数为city=城市名称
尝试获取南昌的天气预报
http://wthrcdn.etouch.cn/WeatherApi?city=南昌
得到下面xml数据
java"><?xmlversion="1.0"encoding="utf-8"?>
<resp>
<city>南昌</city>
<updatetime>16:55</updatetime>
<wen>5</wen>
<fengli>3级</fengli>
<shi>90%</shi>
<fengxiang>北风</fengxiang>
<sunrise_1>06:54</sunrise_1>
<sunset_1>17:19</sunset_1>
<sunrise_2/>
<sunset_2/>
<environment>
<aqi>26</aqi>
<pm25>13</pm25>
<suggest>各类人群可自由活动</suggest>
<quality>优</quality>
<MajorPollutants/>
<o3>44</o3>
<co>1</co>
<pm10>14</pm10>
<so2>4</so2>
<no2>27</no2>
<time>16:00:00</time>
</environment>
<yesterday>
<date_1>4日星期五</date_1>
<high_1>高温11℃</high_1>
<low_1>低温7℃</low_1>
<day_1>
<type_1>阴</type_1>
<fx_1>无持续风向</fx_1>
<fl_1>微风</fl_1>
</day_1>
<night_1>
<type_1>小到中雨</type_1>
<fx_1>北风</fx_1>
<fl_1>3-4级</fl_1>
</night_1>
</yesterday>
<forecast>
<weather>
<date>5日星期六</date>
<high>高温7℃</high>
<low>低温5℃</low>
<day>
<type>中到大雨</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</day>
<night>
<type>小到中雨</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</night>
</weather>
<weather>
<date>6日星期天</date>
<high>高温10℃</high>
<low>低温5℃</low>
<day>
<type>阴</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</day>
<night>
<type>多云</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</night>
</weather>
<weather>
<date>7日星期一</date>
<high>高温13℃</high>
<low>低温6℃</low>
<day>
<type>多云</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</day>
<night>
<type>晴</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</night>
</weather>
<weather>
<date>8日星期二</date>
<high>高温13℃</high>
<low>低温7℃</low>
<day>
<type>晴</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</day>
<night>
<type>多云</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</night>
</weather>
<weather>
<date>9日星期三</date>
<high>高温13℃</high>
<low>低温10℃</low>
<day>
<type>小雨</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</day>
<night>
<type>小雨</type>
<fengxiang>无持续风向</fengxiang>
<fengli>微风级</fengli>
</night>
</weather>
</forecast>
<shus>
<shu>
<name>晨练指数</name>
<value>不宜</value>
<detail>有较强降水,请避免户外晨练,建议在室内做适当锻炼,保持身体健康。</detail>
</shu>
<shu>
<name>舒适度</name>
<value>较舒适</value>
<detail>白天有雨,人们会感到有些凉意,但大部分人完全可以接受。</detail>
</shu>
<shu>
<name>穿衣指数</name>
<value>较冷</value>
<detail>建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。</detail>
</shu>
<shu>
<name>感冒指数</name>
<value>极易发</value>
<detail>将有一次强降温过程,天气寒冷,且空气湿度较大,极易发生感冒,请特别注意增加衣服保暖防寒。</detail>
</shu>
<shu>
<name>晾晒指数</name>
<value>不宜</value>
<detail>有较强降水,不适宜晾晒。若需要晾晒,请在室内准备出充足的空间。</detail>
</shu>
<shu>
<name>旅游指数</name>
<value>较不宜</value>
<detail>天气稍凉,有微风,同时有有较强降水,会给出行产生很多麻烦,建议好还是多选择在室内活动!。</detail>
</shu>
<shu>
<name>紫外线强度</name>
<value>最弱</value>
<detail>属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。</detail>
</shu>
<shu>
<name>洗车指数</name>
<value>不宜</value>
<detail>不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。</detail>
</shu>
<shu>
<name>运动指数</name>
<value>较不宜</value>
<detail>有较强降水,建议您选择在室内进行健身休闲运动。</detail>
</shu>
<shu>
<name>约会指数</name>
<value>不适宜</value>
<detail>较强降水天气会给室外约会增添许多麻烦,尽量不要外出约会,最好在室内促膝谈心。</detail>
</shu>
<shu>
<name>雨伞指数</name>
<value>带伞</value>
<detail>有较强降水,您在外出的时候一定要带雨伞,以免被雨水淋湿。</detail>
</shu>
</shus>
</resp>
得到数据后使用XML解析数据即可,最好是建立一个实体类来存储数据
下面是使用DOM方式解析部分数据代码
DocumentBuilderbuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
//is是网络连接得到的输入流
Documentdoc=builder.parse(is);
if(doc==null)thrownewException();
Elementelement=doc.getDocumentElement();
NodeListchildList=element.getChildNodes();
for(inti=0;i<childList.getLength();i++){
Elemente=(Element)childList.item(i);
Stringtag=e.getTagName();
Stringtext=e.getTextContent();
if("city".equals(tag)){
//城市
}elseif("updatetime".equals(tag)){
//更新时间
}elseif("wen".equals(tag)){
//即时温度
}elseif("fengli".equals(tag)){
//风力
}elseif("shi".equals(tag)){
//湿度
}elseif("fengxiang".equals(tag)){
//风向
}
}
㈡ android 做一个天气预报的步骤
安卓编程设计很多方面,非常复杂,需要系统的学习才可以,这里以一个简单的天气预报app编程为例:
public class WebServiceUtil
{
// 定义Web Service的命名空间
static final String SERVICE_NS = "http://WebXml.com.cn/";
// 定义Web Service提供服务的URL
static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
public static List getProvinceList()
{
// 需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
String methodName = "getRegionProvince";
// 创建HttpTransportSE传输对象
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
// 使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
envelope.bodyOut = soapObject;
// 设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return parseProvinceOrCity(detail);
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static List getCityListByProvince(String province)
{
// 需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数)
String methodName = "getSupportCityString";
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
soapObject.addProperty("theRegionCode", province);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return parseProvinceOrCity(detail);
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static List parseProvinceOrCity(SoapObject detail)
{
ArrayList result = new ArrayList();
for (int i = 0; i < detail.getPropertyCount(); i++)
{
String str = detail.getProperty(i).toString();
// 解析出每个省份
result.add(str.split(",")[0]);
}
return result;
}
public static SoapObject getWeatherByCity(String cityName)
{
// 根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
String methodName = "getWeather";
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
soapObject.addProperty("theCityCode", cityName);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return detail;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
㈢ android 如何实现获取天气预报信息
方法步骤(以安卓5.0为例)
一、打开GPS
九、提示
1.适时刷新天气需要开启GPS定位。
2.在没有WIFI的地方刷新天气需要消耗一定的流量,刷新频率越高,消耗流量越多。
㈣ 安卓手机手机桌面那个天气预报在哪里设置
以华为p10手机为例,方法如下:
1、长按手机桌面空白的地方,如下图。
㈤ 如何在android实现调用网上的天气预报
下个GO天气,在桌面选项里有小插件,放到桌面开启自动更新就可以了。