首页 区号查询 区号查询示例代码 城市查区号[Java]

城市查区号示例代码[Java]

作者:xiezhongpian 阅读数:2950 上传时间:2017-05-09

城市查区号

package api.jisuapi.areacode;

import java.net.URLEncoder;

import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class City2code {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/areacode/city2code";
	public static final String city = "杭州";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&city=" + URLEncoder.encode(city,"utf-8");

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONArray resultarr = json.optJSONArray("result");
				for (int i = 0; i < resultarr.size(); i++) {
					JSONObject obj = (JSONObject) resultarr.opt(i);
					String province = obj.getString("province");
					String city = obj.getString("city");
					String town = obj.getString("town");
					String areacode = obj.getString("areacode");
					System.out.println(province + " " + city + " " + town + " " + areacode);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}