java – 尝试Catch意外令牌

前端之家收集整理的这篇文章主要介绍了java – 尝试Catch意外令牌前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
代码在try和catch上给出了意外令牌的错误.怎么了?
  1. public class WeatherTest
  2. {
  3.  
  4. String weatherurl = "http://weather.yahooapis.com/forecastRSS?w=35801&u=c";
  5. HttpClient httpClient = new DefaultHttpClient();
  6. HttpGet httpGet = new HttpGet(weatherurl);
  7.  
  8. try {
  9.  
  10. HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
  11.  
  12. InputStream inputStream = httpEntity.getContent();
  13. Reader in = new InputStreamReader(inputStream);
  14. BufferedReader bufferedreader = new BufferedReader(in);
  15. StringBuilder stringBuilder = new StringBuilder();
  16.  
  17. String stringReadLine = null;
  18.  
  19. while ((stringReadLine = bufferedreader.readLine()) != null)
  20. {
  21. stringBuilder.append(stringReadLine + "\n");
  22. }
  23.  
  24. String qResult = stringBuilder.toString();
  25.  
  26. }
  27. catch (IOException ie)
  28. {
  29. ie.printStackTrace();
  30. }
  31. }

解决方法

您的try / catch块不在方法内.

您可以将其放在方法中,然后调用方法.

  1. public class WeatherTest {
  2. String weatherurl = "http://weather.yahooapis.com/forecastRSS?w=35801&u=c";
  3. HttpClient httpClient = new DefaultHttpClient();
  4. HttpGet httpGet = new HttpGet(weatherurl);
  5.  
  6. public void myMethod() {
  7. try { ... }
  8. catch { ... }
  9. }
  10. }

猜你在找的Java相关文章