manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gusfree.naver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".NaverOpenAPIActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
NaverOpenAPIActivity.java
package com.gusfree.naver;

import java.io.InputStream;
import java.net.URL;

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

import org.w3c.dom.Document;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;



public class NaverOpenAPIActivity extends Activity {
	ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		listView=(ListView) findViewById(R.id.listView1);

		String sample =
"http://openapi.naver.com/search?key=자신의 키값&query=starwars&display=10&start=1&target=movie";

		try {
			URL url = new URL(sample);
			InputStream is = url.openStream();
			//Dom Parser, Document Object

			DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();

			DocumentBuilder builder=factory.newDocumentBuilder();

			//데이터를 읽어서 다큐먼트를 만드느다.
			Document document = builder.parse(is);

			//<total>태그의 value 값 (total태그는 1개 뿐이다)
			String total=document.getElementsByTagName("total").item(0).
					getFirstChild().getNodeValue();

			Log.i("검색결과 갯수","검색결과 갯수"+total);

			for(int i=0;i<Integer.parseInt(total);i++){
				//!title은 최초에
				//<title>Naver Open API - movie ::'starwars'</title>
				//에 나오기 대문에 i+1번째로 보기로 하자
				String title=document.getElementsByTagName("title").item(0).
						getFirstChild().getNodeValue();
				/*<actor> 태그 value를 전부 Log찍기*/
				String actor=document.getElementsByTagName("actor").item(0).
						getFirstChild().getNodeValue();

				Log.i("for문","title= "+title);
				Log.i("for문","actor= "+actor);
			}

			/*모든 태그가 열고 닫힘이 100%일치해야 사용가능*/
		
			/*//item 10개가 들어있다
			NodeList itemList = document.getElementsByTagName("item");

			for(int i=0;i<itemList.getLength();i++){
				Node itemNode = itemList.item(i);
				NodeList itemChildList = itemNode.getChildNodes();
				for(int j=0;j<itemChildList.getLength();j++){
					//tite,link,image,subtitle.....
					String text= itemChildList.item(j).getFirstChild().getNodeValue();

					Log.i("텍스트","텍스트: "+text);
				}
				Log.w("다음 item","다음 item");

			}*/
		} catch (Exception e) {
		}
	}
}



'Android > 2012.04월 강좌' 카테고리의 다른 글

11일차 SocketServer (Client,Server C->S, S->C)  (0) 2012.05.10
11일차 Json  (0) 2012.05.10
11일차 DOM과 SAX의 기술적 특징  (0) 2012.05.10
11일차 오픈 API 사용법  (0) 2012.05.10
11일차 Weather (SAX parser)  (0) 2012.05.10

+ Recent posts