안드로이드

팁 #5 - HTML 소스 코드 가져오기

raulyo 2012. 4. 16. 13:23

구글링을 해 보면 HTML 소스 가져오는게 몇개 있고..이곳 게시판에도 있지만..
코드가 복잡해서... 그중 가장 직관직인 코드를 올려봅니다.
출처는...죄송합니다...구글링 너무 하다가...링크를 까먹었네요.
원작자가 보시구....원하지 않으시면 삭제하겠습니다. ^^;

아래 iStart, iEnd 값은 제 목적에 맞춰진 것이므로..알아서 변경하세요.


 private String HTMLbyKeyword()
 {
  int iStart = 0;
  int iEnd = 0;
  String documentURL = "http://www.yourdomain.com/index.htm";
  String source = "";
  StringBuffer url_content = new StringBuffer();
  try 
  {
   
   URL url = new URL(documentURL);
   InputStream is = url.openStream();
   InputStreamReader isr = new InputStreamReader(is);
   BufferedReader br = new BufferedReader(isr);   
     
   String inStr = "";
   while((inStr = br.readLine()) != null) {
    url_content.append(inStr+"\n");
   }   
  
   source = new String(url_content);
   iStart = source.indexOf(  your keyword );
   iEnd = source.indexOf( ">", iStart);
   
   
  }
  catch(Exception e) 
  {
   // your code.
  }
  return source.substring(iStart+5, iEnd-1);
 }
 

 
댓글
2010.09.01 23:40:52
동네가수
아파치 HttpCore API 이용법 추가해드립니다.
자바 API의 URL보다는 이쪽이 유용한 메서드들이 많기때문에 이쪽을 활용하실분들은 이렇게 하시면 됩니다.

HttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet("http://www.yourdomain.com/index.htm");
HttpResponse response = client.excute(method);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
댓글
2010.09.16 21:39:29
id: 달토끼달토끼

감사합니다.