Android天气预报之基于HttpGet对象解析天气数据的方法
程序员文章站
2022-05-16 12:30:13
本文实例所述为android天气预报之解析天气数据的代码,可实现获取httpget对象读取天气网站天气数据,并从数据中解析出天气数据,比如温度、温度、风力、风向、未来几天天...
本文实例所述为android天气预报之解析天气数据的代码,可实现获取httpget对象读取天气网站天气数据,并从数据中解析出天气数据,比如温度、温度、风力、风向、未来几天天气趋势、当天天气状况、空气污染指数等信息,还包括了调用对应的图片或天气动画文件,对于开发android天气预报程序的可以参考本文实例。
具体功能代码如下:
import java.io.ioexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.list; import java.util.locale; import net.tsz.afinal.finalhttp; import net.tsz.afinal.http.ajaxcallback; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonexception; import org.json.jsonobject; import org.lmw.weather.myapp; import org.lmw.weather.entity.weatherentity; import android.app.activity; import android.os.handler; import android.os.message; /** * 解析天气数据 * @author dave */ public class weatherdata { private activity activity; private finalhttp fh; public static string def_weather_key="def_weather"; public weatherdata(activity activity) { this.activity = activity; fh = new finalhttp(); fh.configtimeout(1000 * 3); } public void getdata(final string cityid,final handler hd) { stringbuffer sb_url = new stringbuffer(); sb_url.append("http://0.qnweather.duapp.com/weather.php?uri="); sb_url.append("http://m.weather.com.cn/data/"); sb_url.append(cityid); sb_url.append(".html"); final message msg=new message(); fh.get(sb_url.tostring(), new ajaxcallback() { @override public void onsuccess(object t) { super.onsuccess(t); mysharedpreferences.writemessage(activity, "def_weather",t.tostring()); msg.what=0; msg.obj=parsejson(t.tostring()); hd.sendmessage(msg); } @override public void onfailure(throwable t, int errorno, string strmsg) { super.onfailure(t, errorno, strmsg); system.out.println("-------errorno---------"+errorno); msg.what=-1; msg.arg1=errorno; msg.obj=mysharedpreferences.readmessage(activity, def_weather_key, ""); hd.sendmessage(msg); } }); } private string connserverforresult(string strurl) { // 获取httpget对象 httpget httprequest = new httpget(strurl); string strresult = ""; try { // httpclient对象 httpclient httpclient = new defaulthttpclient(); // 获得httpresponse对象 httpresponse httpresponse = httpclient.execute(httprequest); if (httpresponse.getstatusline().getstatuscode() == httpstatus.sc_ok) { // 取得返回的数据 strresult = entityutils.tostring(httpresponse.getentity()); } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } system.out.println("rresult" + strresult); return strresult; // 返回结果 } // 数据解析 private weatherentity parsejson(string strresult) { weatherentity weather = null; try { jsonobject jsonobj = new jsonobject(strresult.replace("℃", "°")) .getjsonobject("weatherinfo"); weather = new weatherentity(); int ftime = jsonobj.getint("fchh"); // 更新时间(整点)【更新时间确定temp属于哪天】 int temp = 0; // 偏移 if (ftime >= 18 || ftime < 8) { weather.setnight(true); temp = 1; } myapp.week = jsonobj.getstring("week");// 今天星期几 weather.setcity(jsonobj.getstring("city")); // 城市 weather.setcomfortable(jsonobj.getstring("index")); // 舒适度 weather.setrefreshdate(getdate()); // 更新日期 weather.setrefreshtime(gettime()); // 更新时间 weather.setrefreshweek(getweek()); // 更新星期 weather.setpicindex(jsonobj.getint("img1")); // 当天天气图片编号 list<integer> toppic = new arraylist<integer>(); // 最高温时的图片编号 if (temp == 1) { toppic.add(getsavepic(activity)); } else { toppic.add(getjsonpic(jsonobj, "img", 1 + temp)); savepic(activity, toppic.get(0)); } toppic.add(getjsonpic(jsonobj, "img", 3 - temp)); toppic.add(getjsonpic(jsonobj, "img", 5 - temp)); toppic.add(getjsonpic(jsonobj, "img", 7 - temp)); weather.settoppic(toppic); list<integer> lowpic = new arraylist<integer>(); // 最低温时的图片编号 lowpic.add(getjsonpic(jsonobj, "img", 2 - temp)); lowpic.add(getjsonpic(jsonobj, "img", 4 - temp)); lowpic.add(getjsonpic(jsonobj, "img", 6 - temp)); lowpic.add(getjsonpic(jsonobj, "img", 8 - temp)); weather.setlowpic(lowpic); // ---------------------------以上为获取图片编号,暂且不管---------------------------------------------------------------------- list<string> templist = new arraylist<string>(); // 未来五天温度(第一个是今天) templist.add(jsonobj.getstring("temp1")); templist.add(jsonobj.getstring("temp2")); templist.add(jsonobj.getstring("temp3")); templist.add(jsonobj.getstring("temp4")); templist.add(jsonobj.getstring("temp5")); templist.add(jsonobj.getstring("temp6")); myapp.templist.clear(); myapp.templist = templist; list<string> weatherlist = new arraylist<string>();// 未来五天天气(第一个是今天) weatherlist.add(jsonobj.getstring("weather1")); weatherlist.add(jsonobj.getstring("weather2")); weatherlist.add(jsonobj.getstring("weather3")); weatherlist.add(jsonobj.getstring("weather4")); weatherlist.add(jsonobj.getstring("weather5")); weatherlist.add(jsonobj.getstring("weather6")); myapp.weatherlist.clear(); myapp.weatherlist = weatherlist; list<string> templistmax = new arraylist<string>(); // 未来五天最高温度集合(有°符号) if (temp == 1) { templistmax.add(getsavetemperature(activity)); } else { templistmax .add(gettemperaturemaxandmin(templist.get(0))[0 + temp]); savetemperature(activity, templistmax.get(0)); } templistmax .add(gettemperaturemaxandmin(templist.get(1 - temp))[0 + temp]); templistmax .add(gettemperaturemaxandmin(templist.get(2 - temp))[0 + temp]); templistmax .add(gettemperaturemaxandmin(templist.get(3 - temp))[0 + temp]); weather.settemperaturemax(templistmax); weather.settodaytemperature(gettemperaturemaxandmin(templist.get(0))[0]); // 当天温度(实时) weather.settodayweather(jsonobj.getstring("img_title1")); // 当天天气描述(实时) list<string> templistmin = new arraylist<string>(); // 未来四天最低温度集合(有°符号) templistmin.add(gettemperaturemaxandmin(templist.get(0))[1 - temp]); templistmin.add(gettemperaturemaxandmin(templist.get(1))[1 - temp]); templistmin.add(gettemperaturemaxandmin(templist.get(2))[1 - temp]); templistmin.add(gettemperaturemaxandmin(templist.get(3))[1 - temp]); weather.settemperaturemin(templistmin); weather.settomorrowtemperature(templist.get(1)); // 明天温度(包括最高温和最低温) if (temp == 1) { weatherlist.add(getsaveweather(activity)); } else { weatherlist.add(jsonobj.getstring("weather" + 1)); saveweather(activity, weatherlist.get(0)); } weatherlist.add(jsonobj.getstring("weather" + (2 - temp))); weatherlist.add(jsonobj.getstring("weather" + (3 - temp))); weatherlist.add(jsonobj.getstring("weather" + (4 - temp))); weather.setweather(weatherlist); weather.settomorrowweather(weatherlist.get(1)); list<string> windlist = new arraylist<string>(); // 未来四天风力 windlist.add(jsonobj.getstring("wind1")); windlist.add(jsonobj.getstring("wind2")); windlist.add(jsonobj.getstring("wind3")); windlist.add(jsonobj.getstring("wind4")); weather.setwind(windlist); weather.setmaxlist(transplate(templistmax)); // 未来四天最高温度集合(无°符号) weather.setminlist(transplate(templistmin)); // 未来四天最低温度集合(无°符号) } catch (jsonexception e) { e.printstacktrace(); } return weather; } // 获取更新日期 并转换为(x月x日 周x) private string getdate() { simpledateformat sdf = new simpledateformat("mm月dd日 eee", locale.china); string date = sdf.format(new java.util.date()); system.out.println(date); return date; } // 获取更新时间 并转换为 (小时:分钟 更新) private string gettime() { simpledateformat sdf = new simpledateformat("hh:mm", locale.china); string time = sdf.format(new java.util.date()) + " " + "更新"; system.out.println(time); return time; } private string getweek() { return null; } // 获取最高温度和最低温度,有°符号 private string[] gettemperaturemaxandmin(string str) { return str.split("~"); } // 去除最高温度和最低温度里的°符号 private list<integer> transplate(list<string> strlist) { list<integer> intlist = new arraylist<integer>(); for (string temp : strlist) { intlist.add(integer.valueof(temp.split("°")[0])); } return intlist; } // 获取图片编号 例如"img" + "1" private int getjsonpic(jsonobject jsonobj, string str, int index) throws jsonexception { int result = jsonobj.getint(str + index); if (result == 99 && index > 1) { index--; result = jsonobj.getint(str + index); } return result; } private void savetemperature(activity activity, string value) { // mysharedpreferences mp = new mysharedpreferences(activity); // mp.writemessage("temperature", value); } // 保存的温度 private string getsavetemperature(activity activity) { return mysharedpreferences.readmessage(activity,"temperature", "100"); } private void saveweather(activity activity, string value) { // mysharedpreferences mp = new mysharedpreferences(activity); // mp.writemessage("weather", value); } // 保存的天气 private string getsaveweather(activity activity) { return mysharedpreferences.readmessage(activity,"weather", ""); } private void savepic(activity activity, int value) { // mysharedpreferences mp = new mysharedpreferences(activity); // mp.writemessage("pic", value); } // 保存的天气图片编号 private int getsavepic(activity activity) { return mysharedpreferences.readmessage(activity,"pic", 99); } }
希望本文实例对大家android天气预报程序的开发能够起到一定的帮助作用。