2016-11-18 90 views
0

我正在使用Java在Alexa中创建天气应用程序。在这里,我的目标是这样的。无法知道如何使用会话

  1. 用户输入城市名称/国家名称。
  2. 我的代码决定这是不是一个城市/国家。
  3. 如果是城市,它会给天气,否则会提示用户给城市。
  4. 根据城市,它会给出天气结果。

我的代码如下。

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

我猜测,该API提供了以省会城市为默认如果只有国中给出。在这种情况下,您应该在创建URL之前检查字符串,并且只在字符串不是国家代码时才创建URL请求。

+0

嗨兰斯,谢谢你,我已经检查过了,你确实是直接返回类型是首都,但是根据我的代码,它应该附加城市名和国家代码。 – user3872094

+0

你有链接到API吗?也许你可以包含一个默认值/空值来阻止它返回首都? –

相关问题