안드로이드 개발 질문/답변
(글 수 45,052)
package com.daum.dic.com.daum;
import android.app.Activity;
import android.os.Bundle;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.util.Log;
import android.widget.TextView;
public class dic extends Activity {
private final String MY_DEBUG_TAG = "Daum Dictionary API";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView View_Result = new TextView(this);
try{
URL OpenApi_URL = new URL("http://apis.daum.net/dic/endic?apikey=..&q=child&kind=WORD");
SAXParserFactory SPF = SAXParserFactory.newInstance();
SAXParser SP = SPF.newSAXParser();
XMLReader XR = SP.getXMLReader();
ReadXmlHandler DaumDic = new ReadXmlHandler();
XR.setContentHandler(DaumDic);
XR.parse(new InputSource(OpenApi_URL.openStream()));
ParseXmlData ParseDaumDicXml = DaumDic.getParsedData();
View_Result.setText(ParseDaumDicXml.toString());
}
catch(Exception e){
View_Result.setText("Error : " + e.getMessage());
Log.e(MY_DEBUG_TAG, "DaumDicQueryError", e);
}
this.setContentView(View_Result);
}
}
package com.daum.dic.com.daum;
public class ParseXmlData {
private String title = null;
private String desc = null;
private String link = null;
private String exact_yn = null;
private String pronun = null;
private String target_small_url = null;
public String getTitleString(){
return title;
}
public String getDescString(){
return desc;
}
public String getLinkString(){
return link;
}
public String getExactString(){
return exact_yn;
}
public String getPronunString(){
return pronun;
}
public String getSmallURLString(){
return target_small_url;
}
public void setTitleString(String TitleString){
this.title = TitleString;
}
public void setDescString(String DescString){
this.desc = DescString;
}
public void setLinkString(String LinkString){
this.link = LinkString;
}
public void setExactString(String ExactString){
this.exact_yn = ExactString;
}
public void setPronunString(String PronunString){
this.pronun = PronunString;
}
public void setSmallURLString(String SmallURLString){
this.target_small_url = SmallURLString;
}
public String toString(){
return "Title : " + this.title + "\n\nDescription : " + this.desc + "\n\nLink : " + this.link +
"\n\nExact_yn : " + this.exact_yn + "\n\nPronun : " + this.pronun + "\n\nSmall_URL : " + this.target_small_url;
}
}
package com.daum.dic.com.daum;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ReadXmlHandler extends DefaultHandler{
private boolean tag_item = false;
private boolean tag_title = false;
private boolean tag_desc = false;
private boolean tag_link = false;
private boolean tag_exact_yn = false;
private boolean tag_pronun = false;
private boolean tag_target_small_url = false;
private ParseXmlData ParseDaumDicXml = new ParseXmlData();
public ParseXmlData getParsedData(){
return this.ParseDaumDicXml;
}
@Override
public void startDocument() throws SAXException{
this.ParseDaumDicXml = new ParseXmlData();
}
@Override
public void endDocument() throws SAXException{
//Nothing
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException{
if(localName.equals("item")){
this.tag_item = true;
}
if(localName.equals("title")){
this.tag_title = true;
}
if(localName.equals("description")){
this.tag_desc = true;
}
if(localName.equals("link")){
this.tag_link = true;
}
if(localName.equals("exact_yn")){
this.tag_exact_yn = true;
}
if(localName.equals("pronunciation")){
this.tag_pronun = true;
}
if(localName.equals("target_small_url")){
this.tag_target_small_url = true;
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
if(localName.equals("item")){
this.tag_item = false;
}
if(localName.equals("title")){
this.tag_title = false;
}
if(localName.equals("description")){
this.tag_desc = false;
}
if(localName.equals("link")){
this.tag_link = false;
}
if(localName.equals("exact_yn")){
this.tag_exact_yn = false;
}
if(localName.equals("pronunciation")){
this.tag_pronun = false;
}
if(localName.equals("target_small_url")){
this.tag_target_small_url = false;
}
}
@Override
public void characters(char ch[], int start, int length){
if(this.tag_title){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setTitleString(str.append(ch).toString());
}
if(this.tag_desc){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setDescString(str.append(ch).toString());
}
if(this.tag_link){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setLinkString(str.append(ch).toString());
}
if(this.tag_exact_yn){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setExactString(str.append(ch).toString());
}
if(this.tag_pronun){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setPronunString(str.append(ch).toString());
}
if(this.tag_target_small_url){
StringBuffer str = new StringBuffer();
ParseDaumDicXml.setSmallURLString(str.append(ch).toString());
}
}
}
* 출력결과
*원래 출력되어야하는 것
이렇게 코드를 짰는데요 결과값이 자꾸 이상하게 출력이 됩니다.
offset이랑 query1이 자꾸 나오는데 뭐가 잘못됐는지 잘 모르겠네요.
그리고 target_small_url의 내용을 불러와서 string으로 출력해주고 싶은데 URLConnection을 사용하면 되는 건가요?



