webservice调用天气服务

最近在写大华webservice对接,因为无法实测,所以写了个调用天气的webservice进行测试

 public JSONObject sendReq(String... strings) {
        byte[] content;
        HttpClient httpClient = new HttpClient();
        String deivceListStr = "";
        JSONObject jsonObject = new JSONObject();
        JSONObject deviceTreeJson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray newJsonArray = new JSONArray();
        Document doc = null;
        try {
            String str = XmlUtils.getXML(strings);
            content = str.getBytes("UTF-8");
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
            url="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
            PostMethod method = new PostMethod(url);
            httpClient.getParams().setContentCharset("UTF-8");
            method.setRequestHeader("Connection", "close");
            RequestEntity requestEntity = new ByteArrayRequestEntity(content,
                    "text/xml;charset=UTF-8");
            method.setRequestEntity(requestEntity);
            int statusCode = httpClient.executeMethod(method);
            if (statusCode == 200) {
            //这是调用成功的返回结果,即下方的xmlstr
                //<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getSupportCityResponse xmlns="http://WebXml.com.cn/">
                // <getSupportCityResult><string>北京 (54511)</string><string>上海 (58367)</string><string>天津 (54517)</string><string>重庆 (57516)</string></getSupportCityResult></getSupportCityResponse></soap:Body></soap:Envelope>
                String xmlStr = method.getResponseBodyAsString();
                logger.error("xmlStr打印为" + xmlStr);
                doc = DocumentHelper.parseText(xmlStr);
               
                deviceTreeJson.put("channels", newJsonArray);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deviceTreeJson;
    }
 public static String getXML(String... strings){
 //因为测试,这个方法直接写死了
        xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <web:getSupportCityDataset>" +
                "         <!--Optional:-->" +
                "         <web:theRegionCode>江苏</web:theRegionCode>" +
                "      </web:getSupportCityDataset>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";
        return xml;
    }
Java 11中调用Web服务可以使用Java的内置库和一些第三方库来实现。下面是一种常见的方法来调用Web服务获取天气信息: 1. 首先,你需要创建一个Java项目,并添加相关的依赖库。在Java 11中,你可以使用Java自带的HTTP客户端库来发送HTTP请求。你可以在项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> </dependencies> ``` 2. 创建一个Java类来发送HTTP请求并获取天气信息。你可以使用HttpClient类来发送GET或POST请求,并处理返回的响应数据。以下是一个简单的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class WeatherServiceClient { public static void main(String[] args) { try { // 创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpGet请求 HttpGet httpGet = new HttpGet("http://api.weather.com/weather?city=beijing"); // 发送请求并获取响应 CloseableHttpResponse response = httpClient.execute(httpGet); // 获取响应实体 HttpEntity entity = response.getEntity(); // 将响应实体转换为字符串 String responseString = EntityUtils.toString(entity); // 打印响应结果 System.out.println(responseString); // 关闭响应 response.close(); // 关闭HttpClient httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 替换URL中的"city=beijing"为你想要查询的城市代码或名称。 以上代码示例了如何使用Java 11的HttpClient库发送GET请求并获取响应数据。你可以根据具体的Web服务接口和数据格式进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值