我想读取URL的内容,但如果URL没有响应则不想"挂起".我使用URL创建了一个BufferedReader ...
URL theURL = new URL(url); URLConnection urlConn = theURL.openConnection(); urlConn.setDoOutput(true); BufferedReader urlReader = new BufferedReader(newInputStreamReader(urlConn.getInputStream()));
...然后开始循环阅读内容......
do { buf = urlReader.readLine(); if (buf != null) { resultBuffer.append(buf); resultBuffer.append("\n"); } } while (buf != null);
...但如果读取挂起,则应用程序挂起.
有没有办法,如果没有将代码磨到套接字级别,必要时"超时"读取?
我认为URLConnection.setReadTimeout就是你要找的东西.