2012-05-14 4 views
1

데이터 파싱을위한 페이지의 소스를 얻고 싶었습니다.
내가 다음 코드
안드로이드에서 페이지의 전체 소스를 가져올 수 없습니다.

public static String getPageSourceFromUrl(String Url) { 
     String text = ""; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(Url); 
      HttpResponse response = client.execute(request); 
     InputStream in = response.getEntity().getContent(); 
     BufferedReader reader = new BufferedReader(
       new InputStreamReader(in)); 

     StringBuilder str = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      str.append(line); 
     } 
     in.close(); 
     text = str.toString(); 
    } catch (Exception ex) { 

    } 
    return text; 
} 

을 사용하고 페이지는 다음과 같습니다 (위와 같은) 일부 페이지에서 불행하게도 "http://vn.answers.yahoo.com/question/index?qid=20111022210730AAWvfKI"

, 텍스트 수익은 전체 소스의 단지 부분이다 . 어쩌면 문자열이 한도를 초과하므로 누구나 해결책이 있습니까?

+0

당신이이 문제를 해결하기 위해 관리나요 우아한 방법은 클래스를 만드는 것입니다? – erdomester

+0

죄송합니다. 매우 오래 전 이었으므로 여기서 물어볼 내용을 기억할 수 없습니다. ( –

+0

그렇다면이 문제를 해결할 수있는 제 해결책을 받아주십시오. – erdomester

답변

0

나는 (우리의) 문제를 해결할 수있었습니다. 그런 다음

public class GetRating { 

    public GetRating(){ 

    } 

    public String GetRatingFromURL(String url){ 

     HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client 
     HttpGet httpget = new HttpGet(url); // Set the action you want to do 
     HttpResponse response = null; 
     try { 
      response = httpclient.execute(httpget); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } // Executeit 


     HttpEntity entity = response.getEntity(); 
     InputStream is = null; 


     try { 
      is = entity.getContent(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } // Create an InputStream with the response 


     BufferedReader reader = null; 
     try { 
      reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


     StringBuilder sb = new StringBuilder(); 
     String line = null; 


     try { 
      while ((line = reader.readLine()) != null) // Read line by line 
       sb.append(line + "\n"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     String resString = sb.toString(); // Result is here 


     return resString; 


    } 


} 

다른 클래스에서 호출 :

GetRating GN = new GetRating(); 
String pagesource = GN.GetRatingFromURL(url)("http://somewebpage.com"); 
관련 문제