1.使用百度地图接口,根据ip获取城市名
public static JSONObject getCityInfo(String ip) {
//获取百度的应用ak
String ak = properties.getProperty("baidu_ak");
//百度获取位置的api
String url = properties.getProperty("location_url");
if(StringUtils.isBlank(ak) || StringUtils.isBlank(url)){
return null;
}
Map<String,String> map = new HashMap<>();
map.put("ip", ip);
map.put("ak", ak);
map.put("coor", "BD09LL");
String aString = HttpUtils.doPost(url, map, "UTF8");
JSONObject json = JSONObject.parseObject(aString);
//获取位置信息
String content = json.getString("content");
JSONObject contentJson = JSON.parseObject(content);
return contentJson;
}
@Override
public RequestResult getWeaterInfo(RequestParamas paramas) {
RequestResult result = new RequestResult();
String ip = paramas.getParamate("ip");//获取客户端的ip地址
//
//根据ip获取城市名称
String city= WeatherUtils.getCity(ip);
//根据城市获取天气信息
String info = WeatherUtils.getWeatherData(city);
//获得天气信息的对象
WeatherInfo weatherinfo = WeatherUtils.getWeather(info);
result.addJSONData(weatherinfo);
result.addState(true);
result.addStateMsg("获取天气信息成功");
return result;
}
@Override
public RequestResult getCityInfoByIp(RequestParamas paramas) {
RequestResult result = new RequestResult();
Session session = this.getPDReadingSession();
String ip = paramas.getParamate("ip");
JSONObject contentJson = WeatherUtils.getCityInfo(ip);
//获取位置的详细信息
String address_detail = contentJson.getString("address_detail");
JSONObject addressJson = JSON.parseObject(address_detail);
String province = addressJson.getString("province");
String city = addressJson.getString("city");
String cityCode = addressJson.getString("city_code");
String address = contentJson.getString("address");
SystemConfig systemConfig = (SystemConfig)
session.createCriteria(SystemConfig.class)
.add(Restrictions.eq("configType",
WELCOMEPAGE))
.add(Restrictions.eq("configKey", province))
.uniqueResult();
//欢迎页
String welcomePage = systemConfig.getConfigValue();
ProvinceBean bean = new ProvinceBean();
bean.setAdress(address);
bean.setCity(city);
bean.setCityCode(cityCode);
bean.setProvince(province);
bean.setWelcomePage(welcomePage);
result.addJSONData(bean);
result.addState(true);
result.addStateMsg("获取成功");
return result;
}
/**
* 根据城市获取天气情况
*
* @param cityname
* @return
*/
public static String getWeatherData(String cityname) {
StringBuilder sb=new StringBuilder();;
try {
String weather_url = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;
URL url = new URL(weather_url);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码
BufferedReader reader = new BufferedReader(isr);
String line = null;
while((line=reader.readLine())!=null)
sb.append(line+" ");
reader.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(sb.toString());
return sb.toString();
}