Java – attempt catch unexpected token
•
Java
This code gives an unexpected token error on try and catch What's up?
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();
}
}
Solution
Your try / catch block is not in the method
You can put it in the method and then 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 { ... }
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
