在这里我们采用HTTP请求的方式调用webService接口来获取城市天气预报。
首先在Service层写一个获取天气预报list的接口
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
/**
* 通过HTTP POST请求获取某个城市的天气List集合
* @author user
*
*/
@Service
public class WeatherService {
public List<String> getWeatherList(String city) {
try {
// 第一步:创建服务地址,不是WSDL地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
// 第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 第三步:设置参数
// 3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
// 3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
// 3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
// 第四步:组织SOAP数据,发送请求
System.out.println(city);
String soapXML = getXML(city);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
os.flush();
os.close();
// 第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {// 表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
List<String> list = new ArrayList<String>();
String str = sb.toString();
str = new String(str.getBytes(), "UTF-8");
//因为我们接受到的是一个xml格式的字符串
//我直接采用截取字符串的方式获取天气预报信息
while (str.contains("<string>")) {
String s = str.substring(str.indexOf("<string>") + 8, str.indexOf("</string>"));
list.add(s);
str = str.substring(str.indexOf("</string>") + 9);
}
for (String string : list) {
System.out.println(string);
}
is.close();
isr.close();
br.close();
return list;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <?xml version="1.0" encoding="utf-8"?>
* <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap=
* "http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>
* <getWeather xmlns="http://WebXml.com.cn/">
* <theCityCode>string</theCityCode> <theUserID>string</theUserID>
* </getWeather> </soap:Body> </soap:Envelope>
*
* @param phoneNum
* @return
*/
private String getXML(String city) {
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>" + "<getWeather xmlns=\"http://WebXml.com.cn/\">" + " <theCityCode>" + city
+ "</theCityCode>" + " <theUserID></theUserID>" + "</getWeather>" + "</soap:Body>" + "</soap:Envelope>";
return soapXML;
}
}
Service层写好以后我们就可以在Controller层去调用该方法了
import java.util.List;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.WeatherService;
/**
* 根据城市查询天气预报
*
* @author user
*
*/
@Controller
public class WeatherController {
@Autowired
private WeatherService weatherService;
/**
* 返回city城市今天的天气情况
*
* @param city
* @return
*/
@RequestMapping("/weather")
@ResponseBody
public String getWeacher(String city) {
try {
city = new String(city.getBytes("iso-8859-1"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
List<String> list = weatherService.getWeatherList(city);
for (String string : list) {
System.out.println(string);
}
JSONObject json = new JSONObject();
if (list != null && list.size() > 0) {
json.put("province", list.get(0));
json.put("city", list.get(1));
json.put("updateDTTM", list.get(4));
json.put("temp", list.get(5));
json.put("weather", list.get(6).substring((list.get(6).lastIndexOf(" ") + 1)));
System.out.println(json.toString());
}
return json.toString();
}
}
这样一个获取天气预报的接口就做好了。