7

此代码在尝试和捕获时给了我一个意外令牌错误。怎么了?

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();
    }
}
4

1 回答 1

23

您的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 { ... }
    }
}
于 2013-05-17T19:37:08.460 回答