我正在使用Java在Alexa中创建一个天气应用程序。我的目标是这样的。
我的代码如下。
private String getTheCurrentWeather(Intent intent, Session session) {
InputStreamReader inputStream = null;
BufferedReader bufferedReader = null;
String text = "";
String result = "";
String cityName = getCityName(intent);
String checkIfItIsACountry = getCountryCode(cityName, session);
String countryCode = (String) session.getAttribute("countryCode");
try {
String line;
URL url = new URL(URL_PREFIX + cityName);
inputStream = new InputStreamReader(url.openStream(), Charset.forName("US-ASCII"));
bufferedReader = new BufferedReader(inputStream);
StringBuilder builder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
text = builder.toString();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(text, new TypeReference<Map<String, Object>>() {
});
Map<String, Double> mainMap = (Map<String, Double>) map.get("main");
String cityN = map.get("name").toString();
String cityNWithjoutSpace = map.get("name").toString().replace(" ", "");
if (cityNWithjoutSpace.equalsIgnoreCase(cityName) && checkIfItIsACountry.length() == 0) {
double x = mainMap.get("temp") - 273.15;
result = "Weather in " + cityN + " is " + df2.format(x) + " degrees Celcius from block one";
} else {
if (checkIfItIsACountry.length() != 0) {
result = "I can't get the weather of entire Country, please give me a city";
result = getTheCurrentWeatherWithCountry(intent, session, countryCode);
} else {
result = "You have not entered a valid city";
}
}
} catch (IOException e) {
text = "";
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(bufferedReader);
}
return result;
}
private String getTheCurrentWeatherWithCountry(Intent intent, Session session, String countryCode2) {
InputStreamReader inputStream = null;
BufferedReader bufferedReader = null;
String text = "";
String result = "";
String cityName = getCityName(intent);
String countryCode = (String) session.getAttribute("countryCode");
try {
String line;
URL url = new URL(URL_PREFIX + cityName + "," + countryCode2);
inputStream = new InputStreamReader(url.openStream(), Charset.forName("US-ASCII"));
bufferedReader = new BufferedReader(inputStream);
StringBuilder builder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
text = builder.toString();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(text, new TypeReference<Map<String, Object>>() {
});
Map<String, Double> mainMap = (Map<String, Double>) map.get("main");
String cityN = map.get("name").toString();
String cityNWithjoutSpace = map.get("name").toString().replace(" ", "");
double x = mainMap.get("temp") - 273.15;
result = "Weather in " + cityN + " is " + df2.format(x) + " degrees Celcius from block two";
} catch (IOException e) {
text = "";
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(bufferedReader);
}
return result;
}
private String getCityName(Intent intent) {
Slot cityName = intent.getSlot(Slot_City);
return cityName.getValue();
}// get Country Code using Locale
public String getCountryCode(String countryName, Session session) {
String result = "";
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
if (countryName.equals(obj.getDisplayCountry().toString())) {
result = obj.getCountry();
}
}
session.setAttribute("countryCode", result);
return result;
}
我的问题是这样的。
当用户将城市作为输入时,它正确地给出了响应,但当用户将输入作为国家,而不是要求用户输入城市时,它直接给出了首都的天气。
请告诉我如何在这里处理会议。
由于
答案 0 :(得分:0)
我猜测,如果仅提供国家/地区,API会将首都城市作为默认值。在这种情况下,您应该在创建URL之前检查字符串,并且只在字符串不是国家/地区代码时才创建URL请求。