您现在的位置是: 首页 > 天气应用 天气应用
多城市天气预报代码_城市天气预报代码查询
zmhk 2024-06-15 人已围观
简介多城市天气预报代码_城市天气预报代码查询 下面,我将用我自己的方式来解释多城市天气预报代码的问题,希望我的回答能够对大家有所帮助。让我们开始讨论一下多城市天气预报代码的话题
下面,我将用我自己的方式来解释多城市天气预报代码的问题,希望我的回答能够对大家有所帮助。让我们开始讨论一下多城市天气预报代码的话题。
1.处理数据批量生成sql插入语句
2.如何使用百度天气预报API接口
处理数据批量生成sql插入语句
处理数据批量生成sql插入语句
最近在做一个天气预报模块,首先需要将客户端公网ip转换成所在城市,然后将所在城市名转换成对应的城市代码,在网上找到了城市代码,但是需要处理一下,看了看,有三百多城市及对应的城市代码,想存到数据库。就想着做一个数据处理自动生成sql语句的工具,提高效率。
1 直辖市
2 "北京","上海","天津","重庆"
3 "101010100","101020100","101030100","101040100"
4
5 特别行政区
6 "香港","澳门"
7 "101320101","101330101"
8
9 黑龙江
10 "哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"
11 "101050101","101050201","101050301","101050901","101050801","101051301","101051201","101051101","101050401","101051002","101050601","101050501","101050701"
12
13 吉林
14 "长春","延吉","吉林","白山","白城","四平","松原","辽源","大安","通化"
15 "101060101","101060301","101060201","101060901","101060601","101060401","101060801","101060701","101060603","101060501"
16
17 辽宁
18 "沈阳","大连","葫芦岛","盘锦","本溪","抚顺","铁岭","辽阳","营口","阜新","朝阳","锦州","丹东","鞍山"
19 "101070101","101070201","101071401","101071301","101070501","101070401","101071101","101071001","101070801","101070901","101071201","101070701","101070601","101070301"
20
21 内蒙古
22 "呼和浩特","呼伦贝尔","锡林浩特","包头","赤峰","海拉尔","乌海","鄂尔多斯","通辽"
23 "101080101","101081000","101080901","101080201","101080601","101081001","101080301","101080701","101080501"
24
25 河北
26 "石家庄","唐山","张家口","廊坊","邢台","邯郸","沧州","衡水","承德","保定","秦皇岛"
27 "101090101","101090501","101090301","101090601","101090901","101091001","101090701","101090801","101090402","101090201","101091101"
28
29 河南
30 "郑州","开封","洛阳","平顶山","焦作","鹤壁","新乡","安阳","濮阳","许昌","漯河","三门峡","南阳","商丘","信阳","周口","驻马店"
31 "101180101","101180801","101180901","101180501","101181101","101181201","101180301","101180201","101181301","101180401","101181501","101181701","101180701","101181001","101180601","101181401","101181601"
32
33 山东
34 "济南","青岛","淄博","威海","曲阜","临沂","烟台","枣庄","聊城","济宁","菏泽","泰安","日照","东营","德州","滨州","莱芜","潍坊"
35 "101120101","101120201","101120301","101121301","101120710","101120901","101120501","101121401","101121701","101120701","101121001","101120801","101121501","101121201","101120401","101121101","101121601","101120601"
36
37 山西
38 "太原","阳泉","晋城","晋中","临汾","运城","长治","朔州","忻州","大同","吕梁"
39 "101100101","101100301","101100601","101100401","101100701","101100801","101100501","101100901","101101001","101100201","101101101"
40
41 江苏
42 "南京","苏州","昆山","南通","太仓","吴县","徐州","宜兴","镇江","淮安","常熟","盐城","泰州","无锡","连云港","扬州","常州","宿迁"
43 "101190101","101190401","101190404","101190501","101190408","101190406","101190801","101190203","101190301","101190901","101190402","101190701","101191201","101190201","101191001","101190601","101191101","101191301"
44
45 安徽
46 "合肥","巢湖","蚌埠","安庆","六安","滁州","马鞍山","阜阳","宣城","铜陵","淮北","芜湖","毫州","宿州","淮南","池州"
47 "101220101","101221601","101220201","101220601","101221501","101221101","101220501","101220801","101221401","101221301","101221201","101220301","101220901","101220701","101220401","101221701"
48
49 陕西
50 "西安","韩城","安康","汉中","宝鸡","咸阳","榆林","渭南","商洛","铜川","延安"
51 "101110101","101110510","101110701","101110801","101110901","101110200","101110401","101110501","101110601","101111001","101110300"
52
53 宁夏
54 "银川","固原","中卫","石嘴山","吴忠"
55 "101170101","101170401","101170501","101170201","101170301"
56
57 甘肃
58 "兰州","白银","庆阳","酒泉","天水","武威","张掖","甘南","临夏","平凉","定西","金昌"
59 "101160101","101161301","101160401","101160801","101160901","101160501","101160701","101050204","101161101","101160301","101160201","101160601"
60
61 青海
62 "西宁","海北","海西","黄南","果洛","玉树","海东","海南"
63 "101150101","101150801","101150701","101150301","101150501","101150601","101150201","101150401"
64
65 湖北
66 "武汉","宜昌","黄冈","恩施","荆州","神农架","十堰","咸宁","襄阳","孝感","随州","黄石","荆门","鄂州"
67 "101200101","101200901","101200501","101201001","101200801","101201201","101201101","101200701","101200201","101200401","101201301","101200601","101201401","101200301"
68
69 湖南
70 "长沙","邵阳","常德","郴州","吉首","株洲","娄底","湘潭","益阳","永州","岳阳","衡阳","怀化","韶山","张家界"
71 "101250101","101250901","101250601","101250501","101251501","101250301","101250801","101250201","101250701","101251401","101251001","101250401","101251201","101250202","101251101"
72
73 浙江
74 "杭州","湖州","金华","宁波","丽水","绍兴","衢州","嘉兴","台州","舟山","温州"
75 "101210101","101210201","101210901","101210401","101210801","101210501","101211001","101210301","101210601","101211101","101210701"
76
77 江西
78 "南昌","萍乡","九江","上饶","抚州","吉安","鹰潭","宜春","新余","景德镇","赣州"
79 "101240101","101240901","101240201","101240301","101240401","101240601","101241101","101240501","101241001","101240801","101240701"
80
81 福建
82 "福州","厦门","龙岩","南平","宁德","莆田","泉州","三明","漳州"
83 "101230101","101230201","101230701","101230901","101230301","101230401","101230501","101230801","101230601"
84
85 贵州
86 "贵阳","安顺","赤水","遵义","铜仁","六盘水","毕节","凯里","都匀"
87 "101260101","101260301","101260208","101260201","101260601","101260801","101260701","101260501","101260401"
88
89 四川
90 "成都","泸州","内江","凉山","阿坝","巴中","广元","乐山","绵阳","德阳","攀枝花","雅安","宜宾","自贡","甘孜州","达州","资阳","广安","遂宁","眉山","南充"
91 "101270101","101271001","101271201","101271601","101271901","101270901","101272101","101271401","101270401","101272001","101270201","101271701","101271101","101270301","101271801","101270601","101271301","101270801","101270701","101271501","101270501"
92
93 广东
94 "广州","深圳","潮州","韶关","湛江","惠州","清远","东莞","江门","茂名","肇庆","汕尾","河源","揭阳","梅州","中山","德庆","阳江","云浮","珠海","汕头","佛山"
95 "101280101","101280601","101281501","101280201","101281001","101280301","101281301","101281601","101281101","101282001","101280901","101282101","101281201","101281901","101280401","101281701","101280905","101281801","101281401","101280701","101280501","101280800"
96
97 广西
98 "南宁","桂林","阳朔","柳州","梧州","玉林","桂平","贺州","钦州","贵港","防城港","百色","北海","河池","来宾","崇左"
99 "101300101","101300501","101300510","101300301","101300601","101300901","101300802","101300701","101301101","101300801","101301401","101301001","101301301","101301201","101300401","101300201"
100
101 云南
102 "昆明","保山","楚雄","德宏","红河","临沧","怒江","曲靖","思茅","文山","玉溪","昭通","丽江","大理"
103 "101290101","101290501","101290801","101291501","101290301","101291101","101291201","101290401","101290901","101290601","101290701","101291001","101291401","101290201"
104
105 海南
106 "海口","三亚","儋州","琼山","通什","文昌"
107 "101310101","101310201","101310205","101310102","101310222","101310212"
108
109 新疆
110 "乌鲁木齐","阿勒泰","阿克苏","昌吉","哈密","和田","喀什","克拉玛依","石河子","塔城","库尔勒","吐鲁番","伊宁"
111 "101130101","101131401","101130801","101130401","101131201","101131301","101130901","101130201","101130301","101131101","101130601","101130501","101131001"
112
113 西藏
114 "拉萨","阿里","昌都","那曲","日喀则","山南","林芝"
115 "101140101","101140701","101140501","101140601","101140201","101140301","101140401"
116
117 台湾
118 "台北","高雄"
119 "101340102","101340201"
城市代码
一看上去很乱的,而且对应关系是每个省城市一行,代码一行,分别用引号引起,用逗号分隔,每行间都没有符号分隔,省名没有用引号。首先是想着把省名去掉,因为每个城市名都是不相同的。想着每两行两行的去处理,但是也要费不少功夫,还容易出错。就想个索性一次性的全处理的算法。
ps:界面很简单,上面是输入数据,中间是转换,下面是输出数据。
后台主要代码:
[csharp] view plain copy
private void button1_Click(object sender, EventArgs e)
{
string data = textBox1.Text.Replace("r", "").Replace("n", "").Replace("t", "").Replace(" ", "").Replace(" ", "").Replace(" ", "");
MatchCollection matchsdata = matches(data, ""[sS]*?"");
string[,] temps = new string[matchsdata.Count / 2, 2];
int count0 = 0;
int count1 = 0;
string input = string.Empty;
foreach (Match m in matchsdata)
{
string tempdata = m.Value.Replace(""", "");
try
{
int tryp = int.Parse(tempdata);
temps[count1, 1] = tempdata;
count1++;
}
catch (Exception ex)
{
temps[count0, 0] = tempdata;
count0++;
}
}
for (int i = 0; i < (matchsdata.Count / 2); i++)
{
input += "insert into tbl_CityCode(c_city,c_code) values( + temps[i, 0] + , + temps[i, 1] + )rn";
}
textBox2.Text = input;
}
public static MatchCollection matches(string str, string exp)
{
return Regex.Matches(str, exp, RegexOptions.IgnoreCase);
}
首先是将输入的数据处理,去除换行符,空格什么的。然后你应该是会得到一行数据,然后通过正则表达式匹配出所有带引号的数据,你会发现需要的数据全部都是用引号引起来的,但是怎样区分城市名和城市代码呢,它们是混在一起的。不用担心,你发现了吗?城市名是字符串,城市代码是一串数字,我们只要将匹配出的数据数组遍历,每一行数据都去转换成int类型,这样城市名的行就会报错,在catch中捕捉,这一行就是城市名,没错的就是城市代码,把数据一次存到一个二维数组,对应的列中就行了。这样就会获得了相对应的城市名和城市代码。生成的sql语句要对应相应的数据库表。
表结构:
转换完了将生成的sql语句放到查询器中执行就ok了。共处理了349个城市。
最后不放心自己的算法,随机抽查了几条数据,没有错误。
<script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首页横幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="/pagead/show_ads.js"></script>
如何使用百度天气预报API接口
发送到这个网站的一个网址,会自动返回此城市的天气信息。对此信息再格式化就好了。
发送:/weather.jsp?city=广州
返回:b4.gif^广州^雷阵雨^26℃ ~ 22℃^微风^紫外线:弱
发送:/weather.jsp?city=上海
返回:b3.gif^上海^阵雨转阴^16℃ ~ 13℃^东南风3-4级^紫外线:最弱
至于如何判断访问者是哪一个城市,需要分析其IP地址然后再加判断吧,你到网上找个IP转城市的数据库看看。
百度API Key申请地址:/apiconsole/key创建应用 如图:?
提交后得到API Key 如图:?
接口实例:/telematics/v3/weather?location=南昌&output=json&ak=你的API Key&mcode=你的数字签名SHA1;com.example.administrator.jsontest(包名)
接口参数说明
参数类型
参数名称
是否必须
具体描述
String location true 输入城市名或经纬度,城市名如北京或者131,经纬度格式为lng,lat坐标,如:location=116.305145,39.982368;全国值为all,返回省会城市自治区,港澳台天气情况多城市天气预报中间用“|”分隔,如:location=116.305145,39.982368|123.342323,36238945|...
String output false 输出的数据格式,默认为xml格式,当output设置为json时,输出的为json数据格式
String coord_type false 请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84;
返回的JSON数据
{
"error":0,
"status":"success",
"date":"2016-03-05",
"results":[
{
"currentCity":"北京",
"pm25":"144",
"index":[
{
"title":"穿衣",
"zs":"较冷",
"tipt":"穿衣指数",
"des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},
{
"title":"洗车",
"zs":"不宜",
"tipt":"洗车指数",
"des":"不宜洗车,未来24小时内有扬沙或浮尘,如果在此期间洗车,极易很快蒙上新的灰尘。"},
{
"title":"旅游",
"zs":"一般",
"tipt":"旅游指数",
"des":"风稍大,扬沙或浮尘天气对能见度和空气质量都会有些影响,出行请注意交通安全和采取适当的防尘措施。"},
{
"title":"感冒",
"zs":"易发",
"tipt":"感冒指数",
"des":"昼夜温差大,风力较强,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},
{
"title":"运动",
"zs":"较不宜",
"tipt":"运动指数",
"des":"有扬沙或浮尘,建议适当停止户外运动,选择在室内进行运动,以避免吸入更多沙尘,有损健康。"},
{
"title":"紫外线强度",
"zs":"最弱",
"tipt":"紫外线强度指数",
"des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}
? ],
?"weather_data":[
?{
?"date":"周六 03月05日 (实时:12℃)", ?"dayPictureUrl":"/images/weather/day/fuchen.png",
"nightPictureUrl":"/images/weather/night/qing.png",
?"weather":"浮尘转晴",
?"wind":"北风4-5级",
?"temperature":"12 ~ -1℃"},
?{
?"date":"周日",
"dayPictureUrl":"/images/weather/day/duoyun.png",
"nightPictureUrl":"/images/weather/night/duoyun.png",
?"weather":"多云",
?"wind":"微风",
?"temperature":"10 ~ -3℃"},
?{
?"date":"周一", "dayPictureUrl":"/images/weather/day/duoyun.png",
"nightPictureUrl":"/images/weather/night/yin.png",
?"weather":"多云转阴",
?"wind":"微风",
?"temperature":"13 ~ 2℃"},
?{
?"date":"周二", "dayPictureUrl":"/images/weather/day/yin.png",
"nightPictureUrl":"/images/weather/night/duoyun.png",
?"weather":"阴转多云",
?"wind":"北风3-4级",
?"temperature":"6 ~ -1℃"}
?]}]}
3. ?我们来写个demo,代码如下:
package com.example.administrator.jsontest;
public class MainActivity extends Activity {
private Button button;
private TextView textView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
String re = (String) msg.obj;
textView.setText(re);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", "点击了Button");
sendRequestWithHttpClient();
}
});
}
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("/telematics/v3/weather?location=南昌&output=json&ak=8ixCCFzlBB617YX7tONI2P5B&mcode=1C:6B:42:33:E8:A6:DC:A2:11:6E:26:EC:84:BD:42:E3:8E:6B:57:9A;com.example.administrator.jsontest");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Log.i("TAG", response.toString()); parseJSONObjectOrJSONArray(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//解析JSON数据
private void parseJSONObjectOrJSONArray(String jsonData) {
try {
String count = "";
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("results");
if (jsonArray.length() > 0) {
JSONObject object = jsonArray.getJSONObject(0);
String city = object.optString("currentCity");
JSONArray array = object.getJSONArray("weather_data");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject1 = array.getJSONObject(i);
String dateDay = jsonObject1.optString("date");
String weather = jsonObject1.optString("weather");
String wind = jsonObject1.optString("wind");
String temperature = jsonObject1.optString("temperature");
count =count +"\n"+ dateDay + " " + weather + " " + wind + " " + temperature;
Log.i("AAA",count);
}
Message message = new Message();
message.what = 0;
message.obj = count;
handler.sendMessage(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
4.?运行结果如下:?
今天关于“多城市天气预报代码”的讨论就到这里了。希望通过今天的讲解,您能对这个主题有更深入的理解。如果您有任何问题或需要进一步的信息,请随时告诉我。我将竭诚为您服务。
上一篇:四川广安邻水天气预报_四川广安邻水天气预报15天查询
下一篇:十五天气预报抚州市