在Java中实际接收数据时处理IOExceptions

时间:2011-11-29 17:26:58

标签: java exception twitter exception-handling httprequest

目前我正在使用http请求正确地从Twitter接收流媒体数据。但是,当我测试它时,例如断开客户端与互联网的连接(在接收数据时),我停止接收数据,这是合理的。但是,我希望能够捕获连接的更改并抛出异常或重新启动该过程。

假设代码如下:

public void streamData() throws IOException{
final URL url = new URL(sample);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
....
....
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while ((character = rd.read()) != -1)
            {
            char ch = (char) character; //writing character by character
            System.out.print(ch);
            //if connection has broken send an error
            }
}

0 个答案:

没有答案