Java实现Web Api接口远程调用文章标题

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);

    }  
}

Api 接口调用大同小异 , 希望对各位有帮助

如有更好的建议 , 请留言,或者

本人邮箱 : kevinddw@126.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值