本文实例讲述了Android编程实现获取新浪天气预报数据的方法。分享给大家供大家参考,具体如下:
新浪天气预报地址:
http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&day=0
其中,city后的城市可用java.net.URLEncoder.encode(“武汉”,” gb2312”);也可以直接写”武汉”,但不能用”wuhan”。Password固定
Day为0表示当天天气,1表示第二天的天气,2表示第三天的天气,以此类推,最大为4。
工具类:
1、定义成员
public final String SINA_URL = "http://php.weather.sina.com.cn/xml.php";
public final String PASSWORD = "DJOYnieT8234jlsK";
public String city;
public String status1;
public String status2;
public String figure1;
public String figure2;
public String direction1;
public String direction2;
public String power1;
public String power2;
public String temperature1;
public String temperature2;
public String tgd;
public String zwx_l;
public String zwx_s;
public String ssd_l;
public String ssd_s;
public String ktk_l;
public String ktk_s;
public String xcz_l;
public String xcz_s;
public String chy_l;
public String chy_shuoming;
public String pollution_l;
public String pollution_s;
public String gm_l;
public String gm_s;
public String yd_l;
public String yd_s;
2、获取天气数据
public void UpdateWeatherInfo(String city, String day) {
if (city.equals("")) {
isLoaded = false;
return;
}
String html = null;
try {
html = doGet(SINA_URL + "?city="
+ java.net.URLEncoder.encode(city, "gb2312") + "&password="
+ PASSWORD + "&day=" + day);
Document doc = Jsoup.parse(html);
if (doc.body().getElementsByTag("Profiles").size() == 0) {
isLoaded = false;
return;
}
if (doc.body().getElementsByTag("Profiles").get(0).getElementsByTag("Weather").size() == 0) {
isLoaded = false;
return;
}
Element element = doc.body().getElementsByTag("Profiles").get(0)
.getElementsByTag("Weather").get(0);
this.city = element.getElementsByTag("city").text();
status1 = element.getElementsByTag("status1").text();
status2 = element.getElementsByTag("status2").text();
figure1 = element.getElementsByTag("figure1").text();
figure2 = element.getElementsByTag("figure2").text();
direction1 = element.getElementsByTag("direction1").text();
direction2 = element.getElementsByTag("direction2").text();
power1 = element.getElementsByTag("power1").text();
power2 = element.getElementsByTag("power2").text();
temperature1 = element.getElementsByTag("temperature1").text();
temperature2 = element.getElementsByTag("temperature2").text();
tgd = element.getElementsByTag("tgd").text();
zwx_l = element.getElementsByTag("zwx_l").text();
zwx_s = element.getElementsByTag("zwx_s").text();
ssd_l = element.getElementsByTag("ssd_l").text();
ssd_s = element.getElementsByTag("ssd_s").text();
ktk_l = element.getElementsByTag("ktk_l").text();
ktk_s = element.getElementsByTag("ktk_s").text();
xcz_l = element.getElementsByTag("xcz_l").text();
xcz_s = element.getElementsByTag("xcz_s").text();
chy_l = element.getElementsByTag("chy_l").text();
chy_shuoming = element.getElementsByTag("chy_shuoming").text();
pollution_l = element.getElementsByTag("pollution_l").text();
pollution_s = element.getElementsByTag("pollution_s").text();
gm_l = element.getElementsByTag("gm_l").text();
gm_s = element.getElementsByTag("gm_s").text();
yd_l = element.getElementsByTag("yd_l").text();
yd_s = element.getElementsByTag("yd_s").text();
isLoaded = true;
} catch (UnsupportedEncodingException e) {
isLoaded = false;
}
}
3、访问网络
public static final String ENCODE = "utf-8";
public static String doGet(String url) {
try {
HttpGet httpGet = new HttpGet(url);
HttpClient hc = new DefaultHttpClient();
HttpResponse ht = hc.execute(httpGet);
if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity he = ht.getEntity();
InputStream is = he.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String response = "";
String readLine = null;
while ((readLine = br.readLine()) != null) {
response = response + readLine;
}
is.close();
br.close();
return response;
} else {
return "error";
}
} catch (Exception e) {
return "error";
}
}
4、关于jsoup可参考:
http://baike.baidu.com/view/4066913.htm
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:用Java实现全国天气预报的api接口调用示例ASP.NET使用WebService实现天气预报功能PHP Ajax JavaScript Json获取天气信息实现代码Jquery获取当前城市的天气信息Android天气预报app改进版PHP微信开发之查询城市天气js获取新浪天气接口的实现代码JS显示日历和天气的方法编写简易Android天气应用的代码示例asp.net实现根据城市获取天气预报的方法简单易懂的天气插件(代码分享)