2016-11-18 1 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へのリンクはありますか?おそらく、それが首都を返すことを防ぐために含めることができるデフォルト値/ヌル値がありますか? –

関連する問題