java – 尝试Catch意外令牌

前端之家收集整理的这篇文章主要介绍了java – 尝试Catch意外令牌前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
代码在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();
    }
}

解决方法

您的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 { ... }
    }
}
原文链接:https://www.f2er.com/java/127769.html

猜你在找的Java相关文章