国家气象数据中心Java-api数据对接【Java代码-少平】

版权声明: https://blog.csdn.net/weixin_41722928/article/details/82770636

Java实现Web Api接口远程调用

package com.watering.as.web.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class WebApiReturnJson {

    public static void main(String[] args) throws Exception {  
        
  
        //定义变量属性 后期可采用动态获取
        String dataCode = "NAFP_NWFD_SCMOC";
        String time = "201711231000";
        double minLon = 116.21;
        double maxLon = 116.45;
        double minLat = 41.17;
        double maxLat = 41.33;
        String fcstLevel = "-";
        String fcstEle = "ERH";
        int validTime = 12;
        
        //外接口URL路径拼接
        String urlStr = "http://api.data.cma.cn:8090/api"
                + "?userId=Jlkj_jlkj_user"
                + "&pwd=123456"
                + "&interfaceId=getNafpEleGridInRectByTimeAndLevelAndValidtime"
                + "&dataCode=" + dataCode
                + "&time=" + time
                + "&minLon=" + minLon
                + "&maxLon=" + maxLon
                + "&minLat=" + minLat
                + "&maxLat=" + maxLat
                + "&fcstLevel=" + fcstLevel
                + "&fcstEle=" + fcstEle
                + "&validTime=" + validTime
                + "&dataFormat=json".replace(" ","");
        //链接URL  
        URL url=new URL(urlStr);  
        //返回结果集  
        StringBuffer document = new StringBuffer();  
        //创建链接  
        URLConnection conn = url.openConnection();  
        //读取返回结果集  
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));  
        String line = null;  
        while ((line = reader.readLine()) != null){  
              document.append(line);  
        }     
            reader.close();  
 
        JSONObject json =JSONObject.fromObject(document.toString());  

       

        //获取json中某个对象  

        String str =(String)json.get("requestParams");
        System.out.println(str); 
        
        //由于requestParams包含全部str字符串数据,现将str转Map
        String[] strs = str.split("&");
        Map<String, String> m = new HashMap<String, String>();
        for(String s:strs){
        String[] ms = s.split("=");
        m.put(ms[0], ms[1]);
        }
        System.out.println(m.get("minlon")); 
        
        //获取json串中具体值  后期可根据key随意调取存入数据库
        String obj = (String)json.get("requestTime");  
        System.out.println(obj);
        
    }  
}

Web Api 接口调用大同小异 , 各位看客可参考此类 , 希望对各位有帮助

如有更好的建议 , 请与我联系

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/82770636
今日推荐