Bug修复问题

时间:2022-04-29
本文章向大家介绍Bug修复问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

采用下面的代码,访问网页:http://www.weather.com.cn/data/cityinfo/101010100.html,想读取下图中红框中的内容,但是抛出了IOException,通过各种测试介意确定现在的异常发生在程序中标黄色的地方,也就是reader.readLine()这一句,为什么??

读取网页的代码:

 1 public class HttpUtil {
 2     
 3     public static void sendHttpRequest(final String address, 
 4             final HttpCallBackListener listener) {
 5         new Thread(new Runnable(){
 6             @Override
 7             public void run() {
 8                 HttpURLConnection connection = null ;                
 9                 try {
10                     URL url = new URL(address) ;                    
11                     connection = (HttpURLConnection) url.openConnection() ;            
12                     connection.setRequestMethod("GET");
13                     connection.setConnectTimeout(10000);
14                     connection.setReadTimeout(10000);
15                     InputStream in = connection.getInputStream() ;                    
16                     BufferedReader reader  = new BufferedReader(new InputStreamReader(in)) ;
17                     StringBuilder response = new StringBuilder() ;
18                     String line ;
19 
20                     while((line = reader.readLine()) != null){
21                         response.append(line) ;                    
22                     }
23                     if(listener != null){
24                         //回调onFinish()方法
25                         listener.onFinish(response.toString());
26                     }
27                 } catch (Exception e) {
28                     if(listener != null){
29                         listener.onError(e);
30                     }
31                 }finally{
32                     if(connection != null){
33                         connection.disconnect();
34                     }
35                 }                
36             }            
37         }).start() ;
38     }
39     
40 
41 }