/****************** 검색엔진 선택하기 ***************/
Search_Engine = getString(R.string.daum_api);
daum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Search_Engine = getString(R.string.daum_api);
daum.setAlpha(255);
naver.setAlpha(128);
}
});
naver.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Search_Engine = getString(R.string.naver_api);
naver.setAlpha(255);
daum.setAlpha(128);
}
});
/*****************************************************************/
/****************** jdom 파싱 부분 ******************/
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String format = search.getText().toString();
format = format.replace(" ", "+");
if(format==null || format.equals("") || format == "") {
Toast.makeText(v.getContext(), "검색어를 입력하세요!!!", Toast.LENGTH_LONG).show();
}else {
format = Search_Engine + format;
try {
URL url = new URL(format);
doc = builder.build(url);
Element root = doc.getRootElement();
NodeParser(root);
}catch(JDOMException je) {
je.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
});
/*************************************************************/
void NodeParser(Element root) {
String title = "";
String description = "";
String link = "";
String buf = "<html><body><style>dl {display:inline;}</style></head><body>";
try {
Element channel = root.getChild("channel");
List<Element> list = channel.getChildren();
for(Element channels : list) {
if(channels.getName().equals("total")) {
if(channels.getValue().equals("0")) {
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG).show();
}
}
else if(channels.getName().equals("totalCount")) {
if(channels.getValue().equals("0")) {
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG).show();
}
} else {
if(channels.getName().equals("item")) {
List<Element> channel_list = channels.getChildren();
for(Element items : channel_list) {
if(items.getName().equals("title")) {
title = items.getValue();
}
else if(items.getName().equals("description")) {
description = items.getValue();
}
else if(items.getName().equals("link")) {
link = items.getValue();
}
}
buf += "<dl><a href=" + link + "><dt>" + title + "</dt></a><dl>" + description + "</dl></dl>";
}
}
}
buf += "</body></html>";
webView.loadDataWithBaseURL(null, buf, "text/html", "UTF-8", null);
}catch(Throwable t) {
Toast.makeText(this, "요청 실패: "+t.toString(), 4000).show();
}
}