我正致力于http://webserviceX.NET的示例网络服务,不知怎的,它一直在CDATA内回复它的响应。我正在尝试在groovy中打印我的请求的响应,但它返回null。我在Groovy中练习编码。请耐心等待,因为我刚开始学习语言和SOAP的一切。
这是我的代码:
@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.8.0' )
import wslite.soap.*
class GlobalWeather {
def spUrl = ('http://www.webservicex.net/globalweather.asmx')
def client = new SOAPClient(spUrl)
def getCitiesByCountry(String country) {
def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry"){
body {
GetCitiesByCountry('xmlns': 'http://www.webserviceX.NET') {
CountryName(country)
}
}
}
def retCountry = response.CitiesByCountryResponse.CitiesByCountryResult
return retCountry
}
def getWeather(def city, def country){
def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetWeather"){
body{
GetWeather('xmlns': 'http://www.webserviceX.NET'){
CityName(city)
CountryName(country)
}
}
}
def retCountryCity = response.WeatherResponse.WeatherResult
return retCountryCity
}
static void main(String[] args){
def gWeather = new GlobalWeather()
println gWeather.getCitiesByCountry('UK')
println gWeather.getWeather('Kyiv', 'Ukraine')
}
}
答案 0 :(得分:4)
处理响应时使用了错误的变量名称:
def retCountry = response.CitiesByCountryResponse.CitiesByCountryResult
return retCountry
而不是:
return response.GetCitiesByCountryResponse.GetCitiesByCountryResult
和
def retCountryCity = response.WeatherResponse.WeatherResult
return retCountryCity
而不是:
return response.GetWeatherResponse.GetWeatherResult
此处省略Get
将无效,因为这不是变量的名称(没有getter),而是节点名称。
下面您可以找到更正的脚本:
@Grab(group = 'com.github.groovy-wslite', module = 'groovy-wslite', version = '0.8.0')
import wslite.soap.*
class GlobalWeather {
def spUrl = ('http://www.webservicex.net/globalweather.asmx')
def client = new SOAPClient(spUrl)
def getCitiesByCountry(String country) {
def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry") {
body {
GetCitiesByCountry('xmlns': 'http://www.webserviceX.NET') {
CountryName(country)
}
}
}
return response.GetCitiesByCountryResponse.GetCitiesByCountryResult
}
def getWeather(def city, def country) {
def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetWeather") {
body {
GetWeather('xmlns': 'http://www.webserviceX.NET') {
CityName(city)
CountryName(country)
}
}
}
return response.GetWeatherResponse.GetWeatherResult
}
}
def gWeather = new GlobalWeather()
println gWeather.getCitiesByCountry('UK')
println gWeather.getWeather('Kyiv', 'Ukraine')
P.S。 Upvote准备一个工作的例子!这就是问题的问题。