2013-05-17 4 views
5

このコードは、tryとcatchで予期しないトークンのエラーを表示します。なにが問題ですか?予期しないトークンをキャッチしよう

public class WeatherTest 
{ 

    String weatherurl = "http://weather.yahooapis.com/forecastrss?w=35801&u=c"; 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(weatherurl); 

    try { 

      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity(); 

      InputStream inputStream = httpEntity.getContent(); 
      Reader in = new InputStreamReader(inputStream); 
      BufferedReader bufferedreader = new BufferedReader(in); 
      StringBuilder stringBuilder = new StringBuilder(); 

      String stringReadLine = null; 

      while ((stringReadLine = bufferedreader.readLine()) != null) 
      { 
       stringBuilder.append(stringReadLine + "\n"); 
      } 

      String qResult = stringBuilder.toString(); 

    } 
    catch (IOException ie) 
    { 
     ie.printStackTrace(); 
    } 
} 

答えて

20

try/catchブロックはメソッド内にありません。

メソッドに配置してメソッドを呼び出すことができます。

public class WeatherTest { 
    String weatherurl = "http://weather.yahooapis.com/forecastrss?w=35801&u=c"; 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(weatherurl); 

    public void myMethod() { 
     try { ... } 
     catch { ... } 
    } 
} 
+2

これは私の一番の愚かさです、ありがとう – user673906

+1

それは私たちすべてに起こります。 :) –

+2

@ user673906多分あなたは空気、睡眠、またはコーヒーを必要とします –

関連する問題