Java获取世界各国各城市代码_java获取中国城市代码 中国城市ID

package org.com.Weather.weather;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

/**

* 中央气象台的天气预报API

* */

public class Wweather {

/**

* 获取所有中国 省份及一级城市

* */

public String weather() {

// TODO Auto-generated method stub

String ws_url = "http://m.weather.com.cn/data5/city.xml";

String str= "";

try {

URL url = new URL(ws_url);

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));//解决乱码问题

StringBuffer sb = new StringBuffer();

String s = "";

while ((s = br.readLine()) != null) {

sb.append(s + "\r\n"); //将内容读取到StringBuffer中

}

br.close();

//System.out.println(sb.toString()); 屏幕

str = new String(sb.toString().getBytes());

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return str;

}

/**

* 根据传入参数获取二级城市

* */

public String secondCity(String id){

String ws_url = "http://m.weather.com.cn/data5/city"+id+".xml";

String str= "";

try {

URL url = new URL(ws_url);

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));//解决乱码问题

StringBuffer sb = new StringBuffer();

String s = "";

while ((s = br.readLine()) != null) {

sb.append(s + "\r\n"); //将内容读取到StringBuffer中

}

br.close();

//System.out.println(sb.toString()); 屏幕

str = new String(sb.toString().getBytes());

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return str;

}

/**

* 根据传入参数得到城市天气预报信息ID, 其实也可以直接调用上面方法,为理解方便,故多加一个

* */

public String weatherCityId(String id){

String ws_url = "http://m.weather.com.cn/data5/city"+id+".xml";

String str= "";

try {

URL url = new URL(ws_url);

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));//解决乱码问题

StringBuffer sb = new StringBuffer();

String s = "";

while ((s = br.readLine()) != null) {

sb.append(s + "\r\n"); //将内容读取到StringBuffer中

}

br.close();

//System.out.println(sb.toString()); 屏幕

str = new String(sb.toString().getBytes());

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return str;

}

public static void main(String[] args) {

Wweather w=new Wweather();

String[] strArray=w.weather().split(",");

for(int i=0;i

String[] strArr=strArray[i].split("\\|");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String[] strArray2=w.secondCity(strArr[0]).split(",");

for(int j=0;j

String[] strArray3=w.weatherCityId(strArray2[j].split("\\|")[0]).split(",");

for(int m=0;m

System.out.println(strArray3[m].split("\\|")[1]+" "+w.weatherCityId(strArray3[m].split("\\|")[0]).split("\\|")[1]);

}

}

}

}

}

因需要中国所有城市ID,所以写此程序获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值