日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Webservice调用天气接口案例

發(fā)布時間:2024/3/24 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Webservice调用天气接口案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

廢話不多說,希望能夠幫助到大家。
天氣接口:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
pom.xml配置:

com.sun.jersey
jersey-client
1.18
compile


com.alibaba
fastjson
1.2.24

import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.DefaultClientConfig; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element;import javax.ws.rs.core.MediaType; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;/*** @Description TOO* @Date 2022/8/3 0003 16:43*/ public class Test {public static void main(String[] args) throws DocumentException {post();}/***功能描述 :調(diào)用天氣接口* @date 2022/8/3 0003* @param* @return java.lang.String*/public static String post() throws DocumentException {//創(chuàng)建客戶端Client client = Client.create(new DefaultClientConfig());//指定資源WebResource webResource = client.resource("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");//外網(wǎng)//path:請求的方法,entity請求的參數(shù),type請求的格式,post請求方式(返回的類型)ClientResponse clientResponse = webResource.path("getSupportCity").entity("byProvinceName=湖南").type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class);String xml = clientResponse.getEntity(String.class);System.out.println(xmlToJson(xml));System.out.println(xml);return xml;}/***功能描述 :將返回的xml轉(zhuǎn)化為json* @author LKY* @date 2022/8/3 0003* @param*/public static JSON xmlToJson(String xml) throws DocumentException {List list = new ArrayList<>();//將xml格式的字符串轉(zhuǎn)換成Document對象Document doc = DocumentHelper.parseText(xml);//獲取根節(jié)點Element root = doc.getRootElement();//獲取根節(jié)點下的所有元素List children = root.elements();//循環(huán)所有子元素if(children != null && children.size() > 0) {for(int i = 0; i < children.size(); i++) {Map map1 = new HashMap();Element child = (Element)children.get(i);map1.put(child.getName(), child.getTextTrim());list.add(map1);}}JSONArray jsonObject1 = (JSONArray) JSONObject.toJSON(list);return jsonObject1;} }

總結(jié)

以上是生活随笔為你收集整理的Webservice调用天气接口案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。