加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 尝试Catch意外令牌

发布时间:2020-12-15 03:15:50 所属栏目:Java 来源:网络整理
导读:此代码在try和catch上给出了意外令牌的错误.怎么了? public class WeatherTest{ String weatherurl = "http://weather.yahooapis.com/forecastrss?w=35801u=c"; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(weatherur
此代码在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 { ... }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读