2010. 8. 15. 05:54
HttpClient 및 URL 테스트
2010. 8. 15. 05:54 in 3. Implementation/Android
회차별 로또 당첨번호를 웹에서 가져오는 코드를 구현함에 있어 HttpClient 와 URL 을 이용하는 두가지 방법에 대해서 모두 작성해 보았습니다. 당첨번호를 가져오는 방법은 html 응답을 직접 파싱하여 원하는 당첨번호만 추출하는 방법인데 이는 웹페이지가 변경되면 올바르게 동작하지 않는 문제가 있습니다. 그치만 외부에 공개되지 않은 정보를 웹에서 긁어 올 수 있는 아주 쉬운 방법입니다.
1. HttpClient 를 이용하는 방법
HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://www.645lotto.net/Confirm/number.asp?sltSeq=" + game)); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
2. URL 을 이용하는 방법
URL url = new URL("http://www.645lotto.net/Confirm/number.asp?sltSeq=" + game); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
3. HttpClientTest.java
아래는 작성한 풀 소스입니다. 이 소스를 테스트 하기 위해서는 android.permission.INTERNET 권한이 필요합니다.
package com.sskk.example.HttpClientTest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; public class HttpClientTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayListnumbers = GetLottoNumber1(1); for(Integer number : numbers) { System.out.print(number + " "); } System.out.println(); numbers = GetLottoNumber2(2); for(Integer number : numbers) { System.out.print(number + " "); } System.out.println(); } public static ArrayList GetLottoNumber1(final int game) { ArrayList numberList = new ArrayList (); try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://www.645lotto.net/Confirm/number.asp?sltSeq=" + game)); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String tempStr; StringBuilder htmlSource = new StringBuilder(); while((tempStr = in.readLine()) != null) { htmlSource.append(tempStr); } in.close(); int first = htmlSource.indexOf("class=\"sboll2"); if(first == -1) return null; int last = htmlSource.indexOf("/div>", first); if(last == -1) return null; String numberHtml = htmlSource.substring(first, last); final String search = "../images/Comm/s"; final int length = search.length(); int firstIndex = 0; while(true) { int found = numberHtml.indexOf(search, firstIndex); if(found == -1) break; int dotFound = numberHtml.indexOf(".", found + length); if(dotFound == -1) break; String numberStr = numberHtml.substring(found + length, dotFound); try { Integer number = Integer.parseInt(numberStr); numberList.add(number); // System.out.println(number); } catch(Exception e) { } firstIndex = found + length; } } catch(Exception e) { e.printStackTrace(); } if(numberList.size() != 7) { return null; } return numberList; } public static ArrayList GetLottoNumber2(final int game) { ArrayList numberList = new ArrayList (); try { URL url = new URL("http://www.645lotto.net/Confirm/number.asp?sltSeq=" + game); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String tempStr; StringBuilder htmlSource = new StringBuilder(); while((tempStr = in.readLine()) != null) { htmlSource.append(tempStr); } in.close(); int first = htmlSource.indexOf("class=\"sboll2"); if(first == -1) return null; int last = htmlSource.indexOf("/div>", first); if(last == -1) return null; String numberHtml = htmlSource.substring(first, last); final String search = "../images/Comm/s"; final int length = search.length(); int firstIndex = 0; while(true) { int found = numberHtml.indexOf(search, firstIndex); if(found == -1) break; int dotFound = numberHtml.indexOf(".", found + length); if(dotFound == -1) break; String numberStr = numberHtml.substring(found + length, dotFound); try { Integer number = Integer.parseInt(numberStr); numberList.add(number); // System.out.println(number); } catch(Exception e) { } firstIndex = found + length; } }catch(MalformedURLException e) { } catch(IOException e) { e.printStackTrace(); } if(numberList.size() != 7) { return null; } return numberList; } }