This article illustrates the method of acquiring Sina weather forecast data by Android programming. Share to everyone for your reference, specific as follows:
Sina Weather Forecast Address:
http://php.weather.sina.com.cn/xml.php?city= Wuhan &password=djoyniet8234jlsk&day=0
The city can be Java.net.URLEncoder.encode ("Wuhan", "gb2312") or write "Wuhan" directly, but not "Wuhan". Password fixed
Day 0 indicates the weather, 1 indicates the weather for the following day, 2 indicates the weather on the third day, and so on, and the maximum is 4.
Tool class:
1. Define Members
/** Sina Weather Address * * Public final String Sina_url = "http://php.weather.sina.com.cn/xml.php";
/** sina Weather XML call password/public final String PASSWORD = "Djoyniet8234jlsk";
/** City * * Public String cities;
/** Daytime Weather * * public String Status1;
/** Night Weather * * public String status2;
/** daytime Weather Pinyin */public String figure1;
/** Night Weather Pinyin */public String figure2;
/** Day Wind Direction * * Public String Direction1;
/** Night Wind/public String direction2;
/** Daytime Wind Scale * * public String power1;
/** Night Wind Scale * * public String power2;
/** Daytime Temperature */public String temperature1;
/** Night Temperature * * Public String temperature2;
/** Body Sense Temperature * * Public String TGD;
/** UV Index */public String zwx_l;
/** UV Description */public String zwx_s;
/** Body Sensitivity Index */public String ssd_l;
/** Body Sensitivity Description * * Public String ssd_s;
/** Air Conditioner Index */public String ktk_l;
/** Air Conditioner Description */public String ktk_s;
/** Car Wash Index */public String xcz_l;
/** Wash Car Description * * Public String xcz_s;
/** Clothing Index/public String chy_l;
/** Dress Instructions/public String chy_shuoming;
/** Pollutant diffusion Condition * * public String pollution_l; /** Pollutant Diffusion Condition Description * * Public StrinG pollution_s;
/** Cold Index */public String gm_l;
/** Cold Note * * Public String gm_s;
/** Motion Index/public String yd_l;
/** Motion Description */public String yd_s;
2, get weather data
/** * Update Weather * * @param city * @param Day * 0 means that the weather, 1 means the weather of the next day, 2 means the weather of the third day, and so on, the maximum is 4 * * public void Updateweatherin
Fo (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=" + Pass
WORD + "&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, access to the network
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, about Jsoup can refer to:
Http://baike.baidu.com/view/4066913.htm
I hope this article will help you with the Android program.