beanshell : weatherinfo1.bsh
source("/sdcard/com.googlecode.bshforandroid/extras/bsh/android.bsh"); a = Android(); String iso(String str){ return new String(str.getBytes("UTF-8"),"ISO8859_1");} cityid = a.call("dialogGetInput",iso("天气预报 API"),iso("请输入6位数cityid:"),"280101").get("result"); if (cityid == null){ print("Error: cityid is null"); exit(); } intent = a.call("getIntent"); //print(intent); import java.net.*; import java.io.*; import org.json.*; String getPage(String url){ page = new StringBuffer(); String line; try { Url = new URL(url); conn = Url.openConnection(); ins = new DataInputStream(conn.getInputStream()); while ((line= ins.readLine()) != null){ page.append(line).append("\n"); } ins.close(); } catch(e) { print(e); } return page.toString(); } String utf8(String str){ return new String(str.getBytes("ISO8859_1"),"UTF-8");} url = "http://www.weather.com.cn/data/sk/101"+cityid+".html"; result = getPage(url); res = utf8(result); jso = new JSONObject(res); if (jso.has("weatherinfo")){ JSONObject js = jso.getJSONObject("weatherinfo"); print("城市:"+js.get("city")+js.get("cityid")+" 时间:"+js.get("time")); print("温度:"+js.get("temp")+"C"+" 风向:"+js.get("WD")+" 风力:"+js.get("WS")+" 湿度:"+js.get("SD")); } url = "http://www.weather.com.cn/data/cityinfo/101"+cityid+".html"; result = getPage(url); res = utf8(result); jso = new JSONObject(res); if (jso.has("weatherinfo")){ JSONObject js = jso.getJSONObject("weatherinfo"); print("预报温度:"+js.get("temp1")+"~"+js.get("temp2")+" 天气:"+js.get("weather")); }