前言
现在开发大部分都是服务化或者微服务,数据交换都是跨服务的,这里记录爪哇岛调取其他接口的方法,下面话不多说了,来一起看看详细的介绍吧。
爪哇岛代码如下:
/** * *p类描述:接口读取工具/p */public class ReadUrlUtil { public static JSON object readjsonfrom URL(字符串URL)引发IOException,jsone exception { InputStream is=new URL(网址).openStream();尝试{ BufferedReader rd=new BufferedReader(new InputStreamReader(is,charset。对于名称(' UTF-8 '));StringBuilder sb=new StringBuilder();int cpwhile ((cp=rd.read())!=-1){某人。追加((char)CP);} String jsonText=sb。tostring();JSON对象JSON=JSON对象。from object(JSontext);返回json}最后{ is。close();} } } 测试代码如下:
公共静态void main(字符串[]参数)引发IOException,jsoneexception {/这里调用百度的互联网协议(互联网协议的缩写)定位美国石油学会(美国石油协会)服务详见http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm字符串IP=' 113。57 .244 .100 ';字符串网址='http://api.map.baidu.com/location/ip?AK=32f 38 c 9491 F2 da 9 EB 61106 aab 1 e 9739 IP=' IP ' coor=BD 09 ll;JSON对象JSON=readurlutil。readjsonfrom URL(网址);系统。出去。println(JSON。tostring());System.out.println('经度:'((JSONObject) json.get('content ')).getJSONObject('点')。get(' x ');System.out.println('维度:'((JSONObject) json.get('content ')).getJSONObject('点')。get(' y ');字符串城市=(字符串)((JSON对象)JSON。get(' content ').getJSONObject('address_detail ').get(' city ');city=city.replace('市','');System.out.println(城市);} 测试结果如下图:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。