2013-06-18 2 views
0

웹 사이트에 연결하고 URL을 가져 와서 해당 URL에 연결하여 정보를 얻으려고했습니다. 여기 내 코드가있다.자바에서 다른 http 연결을 엽니 다.

URL url = new URL("http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx"); 
    URLConnection con = url.openConnection(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 

    String l; 
    String Area; 

    Pattern Province = Pattern.compile("Thị xã/T.Phố :"); 
    Pattern City = Pattern.compile("<option(.*?)ue=\"(.*?)\">(.*?)</option>"); 


    while ((l=in.readLine())!=null) { 
     Matcher ProvinceFound = Province.matcher(l); 
     if (ProvinceFound.find()) { 
      while((l=in.readLine())!=null 
        && !l.contains("</select></span>")){ 
       Matcher CityCode = City.matcher(l); 
       if(CityCode.find()){ 
        if(!"0".equals(CityCode.group(2))){ 
         URL url1 = new URL("http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx"); 
         URLConnection con1 = url1.openConnection(); 
         BufferedReader in1 = new BufferedReader(new InputStreamReader(con1.getInputStream())); 

         while((Area=in1.readLine())!=null){ 
          System.out.println(Area); 
          break; 
         } 
        } 

       } 
      } 
     } 
    } 
} 

내가 얻은 결과는 빈 줄뿐입니다. "System.out.println (l);"을 입력하면 여전히 인쇄됩니다. 첫 번째 연결에서 문제가 두 번째 연결이라고 생각합니다.

누구든지 내 코드에서 잘못되었다고 말할 수 있습니까, 정말 고마워요.

+2

표준 Java 명명 규칙을 따르십시오. 변수 (Province, City, ProvinceFound ...)는 대문자로 시작하면 안됩니다. – camickr

+0

코드가 두 번째 연결에 도달 했습니까? 그것을 디버깅하십시오. – fmodos

답변

0

첫 번째 URL을 차단하기 때문에 두 번째 URL에서 읽을 수 없다고 생각합니다. 두 가지 제안 :

  1. 첫 번째 URL의 모든 데이터를 읽고 당신의 희망이 목록에 처리 할 수있는 링크를 추가 읽고 싶은 모든 새로운 URL에 대한 새로운 스레드를 시작
  2. . 그런 다음 기본 URL을 읽는 것을 완료하면 한 번에 하나씩 다른 URL을 읽을 수 있습니다.
+0

감사합니다. Camickr, 옵션 2를 시도했는데 작동합니다! – Mee

관련 문제