출처 : http://blog.naver.com/kippee?Redirect=Log&logNo=130058382142
일반적으로 Java에서 하는 방법과 동일하게 SAX 에서 제시하는 절차를 적용하면 된다
실제 Android에서 적용한 사례를 보면
해당 Site(http://www.google.com/base/feeds) 에 질의할 Query를 조립하고
public ReviewFetcher(String loc, String description, String rat, int start, int numResults) {
Log.v(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG + " location = " + loc + " rating = " + rat + " start = "
+ start + " numResults = " + numResults);
this.start = start;
this.numResults = numResults;
String location = loc; ⓐ 전의 GUI 에서 입력한 지역 (Location) 값
String rating = rat; ⓑ 전의 GUI에서 입력한 음식점 등급
// urlencode params 물론 질의를 위하여 Encoding 필요
try {
if (location != null) {
location = URLEncoder.encode(location, "UTF-8");
}
if (rating != null) {
rating = URLEncoder.encode(rating, "UTF-8");
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// build query
this.query = ReviewFetcher.QBASE;
if ((rating != null) && !rating.equals("ALL")) {
this.query += (ReviewFetcher.QR_PREFIX + rating + ReviewFetcher.QR_SUFFIX);
}
if ((location != null) && !location.equals("")) {
this.query += (ReviewFetcher.QL_PREFIX + location + ReviewFetcher.QL_SUFFIX);
}
if ((description != null) && !description.equals("ANY")) {
this.query += (ReviewFetcher.QD_PREFIX + description + ReviewFetcher.QD_SUFFIX);
}
this.query += (ReviewFetcher.QSTART_INDEX + this.start + ReviewFetcher.QMAX_RESULTS + this.numResults);
Log.v(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG + " query - " + this.query);
}
실제 Site에 질의하여 Parsing 한후 결과를 Array List에 집어 넣는 부분
public ArrayList<Review> getReviews() {
long startTime = System.currentTimeMillis();
ArrayList<Review> results = null;
try {
URL url = new URL(this.query);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ReviewHandler handler = new ReviewHandler(); ⓐ
xr.setContentHandler(handler);
xr.parse(new InputSource(url.openStream()));
// after parsed, get record
results = handler.getReviews();
} catch (Exception e) {
Log.e(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG, e);
}
long duration = System.currentTimeMillis() - startTime;
Log.v(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG + " call and parse duration - " + duration);
return results;
}
}
그리고 실제 SAX 이벤트를 처리하는 Class
package com.msi.manning.restaurant.data;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
/**
* SAX Handler impl for Google Base API and restaurant Review bean.
*
* @author charliecollins
*/
public class ReviewHandler extends DefaultHandler {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static final String ENTRY = "entry";
private static final String R_AUTHOR = "review_author";
private static final String R_CONTENT = "content";
private static final String R_DATE = "review_date";
private static final String R_IMAGE_LINK = "image_link";
private static final String R_LINK = "link";
private static final String R_LOCATION = "location";
private static final String R_NAME = "name_of_item_reviewed";
private static final String R_PHONE = "phone_of_item_reviewed";
private static final String R_RATING = "rating";
private boolean authorChars;
private boolean contentChars;
private boolean dateChars;
private boolean imageLinkChars;
private boolean locationChars;
private boolean nameChars;
private int numEntries;
private boolean phoneChars;
private boolean ratingChars;
생략
첨부문서는 Unblocking Android에 붙어있는 Code이다
☞ 2009.7.22 추가
첨부파일을 집에 와서 Import하여 새로운 Project를 만들려고 하는 데
Eclipse Console 창에 이런 메시지가 뜬다
|
|
|
|
[2009-07-22 21:32:03 - uad-RestaurantFinder] no classfiles specified [2009-07-22 21:32:03 - uad-RestaurantFinder] Conversion to Dalvik format failed with error 1
|
|
|
|
|
날보고 어찌 하라고.
하여튼 2시간에 걸쳐 삽질하다가 풀긴 풀었다.
해결하는 방법은 다음 블로그에...
'Android > Tip&Tech' 카테고리의 다른 글
camera intent 관련 flag 팁 (0) | 2011.06.21 |
---|---|
android Badge 참고사항(카운트) (0) | 2011.06.20 |
[android]listView 항목 드래그앤드랍 하기 (0) | 2011.06.20 |
stateListDrawable (0) | 2011.06.17 |
Sqlite3 에서 Table 이 있는지 없는지 검색하기 (0) | 2011.06.16 |