우선 밑의 소스는 인터넷에서 받은 소스입니다
그리고 질문의 요지는 네이버개발자 센터에 보면
URL을
http://openapi.naver.com/search?key=test&query=%EC%A0%9C%EC%A3%BC%EB%8F%84&target=image&start=1&display=10
이런식으로 넣어주는데
이소스에 보니 다른 방식으로 넣어주더라구요
이게 어떻게 다른건지 알고 싶어서 오늘 계속 찾아봐도 해결이 안되어
질문 올립니다 소스에는 키값도 안들어가 있고 아무튼 xml 파싱 관련인데
알려주세요~
public class XmlParser extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.searchBtn);
final EditText et = (EditText)findViewById(R.id.searchTxt);
//버튼 클릭시 xml을 읽기위한 함수 호출
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String searhTxt = et.getText().toString();
if(searhTxt == ""){
}else{
searchXml(searhTxt);
}
}
});
}
//XmlPullParser를 이용 xml을 읽는다.
public void searchXml(String searchTxt){
String m_searchTxt = "";
try {
m_searchTxt = URLEncoder.encode(searchTxt,"EUC-KR");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String m_sConnectUrl = "http://newssearch.naver.com/search.naver?where=rss&query="+m_searchTxt+"&st=news.all&q_enc=EUC-KR&r_enc=UTF-8&r_format=xml&rp=none&sm=all.basic&ic=all&so=rel.dsc&detail=0&pd=1&start=1&display=20";
//========================주석부분==============================================
// String m_sConnectUrl = "http://openapi.naver.com/search?key=나의키값&query="+m_searchTxt+"&target=news&start=1&display=10";
//================================================ ================================================
Log.e("searchTxt",m_searchTxt);
XmlData xmlData = null;
ArrayList<XmlData> m_xmlData = new ArrayList<XmlData>();
String sTag;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL u = new URL(m_sConnectUrl);
//InputStream in = u.openConnection().getInputStream();
InputStream in = u.openStream();
xpp.setInput(in, "utf-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
// System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
// System.out.println("End document");
} else if(eventType == XmlPullParser.START_TAG) {
Log.e("START_TAG",xpp.getName());
sTag = xpp.getName();
if( sTag.equals("title") ){
// Log.e("title_getText",xpp.nextText());
xmlData = new XmlData();
xmlData.d_title = xpp.nextText() ;
}
if( sTag.equals("link") ){
xmlData.d_link = xpp.nextText();
}
if( sTag.equals("author") ){
// Log.e("title_getText",xpp.nextText());
xmlData.d_author = xpp.nextText() ;
}
// System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
// System.out.println("End tag "+xpp.getName());
sTag = xpp.getName();
if( sTag.equals("item")){
m_xmlData.add(xmlData);
xmlData = null;
}
} else if(eventType == XmlPullParser.TEXT) {
// System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
} catch (Exception e) {
// TODO: handle exception
}
XmlListAdapter adapter = new XmlListAdapter(this, R.layout.row, m_xmlData);
setListAdapter(adapter);
}
//List의 row를 변형하기 위해 Adapter 오버라이딩
private class XmlListAdapter extends ArrayAdapter {
private ArrayList items;
public XmlListAdapter(Context context, int textViewResourceId, ArrayList items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
XmlData xmlData = (XmlData) items.get(position);
if(xmlData != null){
TextView tv1 = (TextView)v.findViewById(R.id.x_title);
TextView tv2 = (TextView)v.findViewById(R.id.x_author);
if(tv1 != null){
//tv1.setText(xmlData.d_title);
tv1.setText(
Html.fromHtml("<a href='"+xmlData.d_link+"'>"
+ xmlData.d_title + "</a>"));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
}
if(tv2 != null){
tv2.setText(xmlData.d_author);
}
}
return v;
}
}
}