bsh for android : 天气预报 API

本文介绍了一段Beanshell脚本,该脚本通过输入城市ID来获取并显示指定城市的实时天气状况及预报信息。脚本从中国气象局网站抓取数据,并利用JSON解析返回的数据。

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"));
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值