天气接口,空气质量接口

1、空气质量接口

  

/**
     * 天气查询
     * place查询地址
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selWeather", method = RequestMethod.POST)
    public Map<String,Object> selWeather(String cityName){
        Map<String, Object> memory = new HashMap<String, Object>();
        String result =null;
        String url ="http://web.juhe.cn:8080/environment/air/cityair";//请求接口地址
        Map params = new HashMap();//请求参数
        params.put("city",cityName);//城市名称的中文名称或拼音,如:上海 或 shanghai
        params.put("key",APPKEY);//APP Key
        try {
            result = PureNetUtil.net(url, params, "GET");
            JSONObject object = JSONObject.fromObject(result);
            if(object.getInt("error_code")==0){
//                System.out.println(object.get("result"));
                memory.put("result",object.get("result"));
            }else{
//                System.out.println(object.get("error_code")+":"+object.get("reason"));
                memory.put("result",object.get("error_code")+":"+object.get("reason"));
            }
            return memory;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return memory;
    }

2、天气查询接口

  接口地址:https://www.sojson.com/open/api/weather/json.shtml?city=%s 

 @ResponseBody
    @RequestMapping(value = "/selWeather1", method = RequestMethod.POST)
    public String selWeather1(@RequestParam(value = "cityName",required = true) String cn){
        try{
            //参数url化
            String city = URLEncoder.encode("上海", "utf-8");
            //拼地址
            String apiUrl = String.format("https://www.sojson.com/open/api/weather/json.shtml?city=%s",city);
            //开始请求
            URL url= new URL(apiUrl);
            URLConnection open = url.openConnection();
            InputStream input = open.getInputStream();
            //这里转换为String
            String result = IOUtils.toString(input,"utf-8");
            System.out.println(result);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

在js中--ajax

/*********************************天气查询 start************************/
function findWeather() {
        $.ajax({
            url:"../../u-route/index/selWeather1.do",
            type:"post",
            async:false,
            data:{"cityName":"上海"},
            success:function(data){
                console.log(JSON.parse(data));
                var jsonData = JSON.parse(data);
                if(jsonData.status=="200"){
                    var dataMsg = jsonData.data.forecast[0];
                    var lowMsg = dataMsg.low;
                    var low = lowMsg.substring(lowMsg.lastIndexOf(" "),lowMsg.length);
                    var highMsg = dataMsg.high;
                    var high = highMsg.substring(highMsg.lastIndexOf(" "),highMsg.length);
                    $("#weaWenDu").html(low + "~" + high);
                    $("#weaImg").html(dataMsg.type);
                    $("#weaCity").html("上海");
                    $("#weaWind").html(dataMsg.fx+",");
                }
            }
        })
        $.ajax({
            url:"../../u-route/index/selWeather.do",
            type:"post",
            async:false,
            data:{"cityName":"上海"},
            success:function(data){
                var quality = data.result[0].citynow.quality;
                $("#weaAir").html("空气质量:"+quality);
            }
        })
}
/**********************************天气查询 end*************************/

 4、天气接口 https 获取ssl

@RequestMapping(value = "/selWeather1", method = RequestMethod.POST)
    public String selWeather1(@RequestParam(value = "cityName",required = true) String cn,String outputStr){
        try{
            //创建SSLContext
            SSLContext sslContext=SSLContext.getInstance("SSL");
            TrustManager[] tm={new MyX509TrustManager()};
            //初始化
            sslContext.init(null, tm, new java.security.SecureRandom());;
            //获取SSLSocketFactory对象
            SSLSocketFactory ssf=sslContext.getSocketFactory();
            //参数url化
            String city = URLEncoder.encode(cn, "utf-8");
            //拼地址                       https://www.sojson.com/open/api/weather/json.shtml?city=%s
            String apiUrl = String.format("https://www.sojson.com/open/api/weather/json.shtml?city=%s",city);
            URL url=new URL(apiUrl);
            HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            //设置当前实例使用的SSLSoctetFactory
            conn.setSSLSocketFactory(ssf);
            conn.connect();
            //往服务器端写内容
            if(null!=outputStr){
                OutputStream os=conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }
            //读取服务器端返回的内容
            InputStream is=conn.getInputStream();
            String result = IOUtils.toString(is,"utf-8");
            return result;
        }catch(Exception e){
            e.printStackTrace();
        }
        return "";
    }
原文地址:https://www.cnblogs.com/zhou-pan/p/8692700.html