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 { ... }
    }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...