[XML 레이아웃] my_open_api.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  
  <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<Spinner  android:id="@+id/sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<EditText android:id="@+id/ed" android:layout_weight="1" android:hint="검색어 입력" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button   android:id="@+id/btn" android:text="검색" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  </LinearLayout>
   
  <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</LinearLayout>

[액티비티] MyOpenApi extends ListActivity

package android.edu;

import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MyOpenApi extends ListActivity {
ArrayList<String> items = new ArrayList<String>();
Spinner sp;
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_open_api);
String url = "http://openapi.naver.com/search"; // 오픈 API URL
String[] targets = {"book", "movieman", "shop"}; // 검색 target 값 설정
sp = (Spinner) findViewById(R.id.sp);// Spinner(콤보박스)에 targets 배열 설정
ed = (EditText)findViewById(R.id.ed);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, targets);
sp.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 스피너를 눌렀을 때 레이아웃 설정
// 버튼 클릭하면 search 실행
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search();
}
});
}
private void search() {
String key = "25536aa0b400d0ec5e01f9453e0bf07a"; // 네이버 오픈API 키
StringBuilder urlStr = new StringBuilder("http://openapi.naver.com/search?"); // HTTP 요청을 보낼 URL과 쿼리스트링 설정 (GET 방식)
urlStr.append("target=" + sp.getSelectedItem().toString());
urlStr.append("&query=" + ed.getText().toString());
urlStr.append("&key=" + key);
HttpGet req = new HttpGet(urlStr.toString());
   HttpClient hc = new DefaultHttpClient();
   try {
ResponseHandler<String> res = new BasicResponseHandler();
String resStr = hc.execute(req, res); // XML결과값
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(resStr)));
NodeList nl = doc.getElementsByTagName("title");
items.clear();
for (int i = 0; i < nl.getLength(); i++) {
Element el = (Element) nl.item(i);
String data = (String) el.getFirstChild().getNodeValue(); // Node값
//String data = el.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
items.add(data);
}
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, items));
} catch (Exception e) {
e.printStackTrace();
}    
}
}
profile