㈠ 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天氣,在桌面選項里有小插件,放到桌面開啟自動更新就可以了。