Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Catch unexpected token

Tags:

java

android

This code gives me an error of unexpected token on the try and catch. What is wrong?

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();
    }
}
like image 973
user673906 Avatar asked May 17 '13 19:05

user673906


1 Answers

Your try/catch block is not inside a method.

You can place it in a method, and call the method.

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 { ... }
    }
}
like image 163
Andy Thomas Avatar answered Oct 06 '22 01:10

Andy Thomas