본문 바로가기

spring/소스코드

○네이버 xml,json 이용한 검색프로그램 파일 만들기

<com.example.domain>-NaverAPI.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.net.*;
import java.io.*;                                                                                                                                                            
public class NaverAPI {
    public static String search(String apiURL) {
        String clientId = "r_XLF45OBi3Xw08BTmvq";//애플리케이션 클라이언트 아이디값";
        String clientSecret = "DjUKEbgt13";//애플리케이션 클라이언트 시크릿값";
        try {
            
            
            //블로그에서 '그린팩토리'를 찍어서 실행하는 프로그램
            
            //String apiURL = "https://openapi.naver.com/v1/search/blog.xml?query="+ text; // xml 결과
            URL url = new URL(apiURL);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("X-Naver-Client-Id", clientId);
            con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
            int responseCode = con.getResponseCode();
            BufferedReader br;
            if(responseCode==200) { // 정상 호출
                br = new BufferedReader(new InputStreamReader(con.getInputStream() , "UTF-8"));
            } else {  // 에러 발생
                br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            }
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = br.readLine()) != null) {
                response.append(inputLine);
            }
            br.close();
            System.out.println(response.toString());
            return response.toString();
        } catch (Exception e) {
            System.out.println(e);
            return e.toString();
        }
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<com.example.web>-<NaverController.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
 
import org.springframework.stereotype.Controller;
 
 
@Controller
public class NaverController {
    //블로그 검색 데이터 출력
    @ResponseBody
    @RequestMapping("blog.json")
    public String bloglist(String keyword, String start) throws Exception{
        
        String text = URLEncoder.encode(keyword, "UTF-8");
        
        String apiURL = "https://openapi.naver.com/v1/search/blog?";
        apiURL +="query=" + text; // json 결과
        apiURL +="&start=" + start;
        apiURL +="&display=5"
        return NaverAPI.search(apiURL);
    }
    //블로그 이동
    @RequestMapping("blog")
    public String blog() throws Exception{
        return "blog";
    }
    //책 검색 데이터 출력
    @ResponseBody
    @RequestMapping("book.json")
    public String booklist(String keyword, String start) throws Exception{
        
        String text = URLEncoder.encode(keyword, "UTF-8");
        
        String apiURL = "https://openapi.naver.com/v1/search/book.json?";
        apiURL +="query=" + text; // json 결과
        apiURL +="&start=" + start;
        apiURL +="&display=5"
        return NaverAPI.search(apiURL);
    }
    //도서 이동
    @RequestMapping("book")
    public String book() throws Exception{
        return "book";
    }
    //xml파일로 책 검색 데이터 출력
    //책 검색 데이터 출력
        @ResponseBody
        @RequestMapping(value="book.xml",produces="application/XML;charset=UTF-8")
        public String bookxml(String searchType,String keyword, String start) throws Exception{
            
            String text = URLEncoder.encode(keyword, "UTF-8");
            
            String apiURL = "https://openapi.naver.com/v1/search/book_adv.xml?";
            apiURL += searchType +"="+ text; // json 결과
            apiURL +="&start=" + start;
            apiURL +="&display=5"
            return NaverAPI.search(apiURL);
        }
    //도서 이동
    @RequestMapping("abook")
    public String abook() throws Exception{
    return "abook";
    }
    
    //xml파일로 장르별 영화 검색 테이터 출력
    
    @ResponseBody
    @RequestMapping(value="movie.xml",produces="application/XML;charset=UTF-8")
    public String moviexml(String genre ,String keyword, String start) throws Exception{
        
        String text = URLEncoder.encode(keyword, "UTF-8");
        
        String apiURL = "https://openapi.naver.com/v1/search/movie.xml?";
        apiURL += "genre=" + genre; // json 결과
        //값을 1,2,3,4..이걸로 줘야함
        apiURL += "&query=" + text;
        apiURL +="&start=" + start;
        apiURL +="&display=5"
        return NaverAPI.search(apiURL);
    }
    //영화 이동
    @RequestMapping("movie")
    public String movie() throws Exception{
    return "movie";
    }
}
 
 
 

 

 

abook.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>


	네이버 도서 상세 검색[네이버 도서 상세 검색] 검색:제목저자출판사  

	{{#each .}}
	<div class="box">
		<img src="{{image}}"author="{{author}}" price="{{price}}" des="{{description}}" width=90 height=120>
		<div class="author" author="{{author}}">저자:{{{author}}}</div>
		<div class="price">가격:{{{price}}}원</div>
	</div>
	{{/each}}
	 더보기 


닫기
var searchType=$("#searchType").val()
	var keyword = $("#keyword").val();
	var start = 1;
	getList();
	$("#btnmore").on("click", function() {
		start = start + 5;
		getList();

	});
	function getList() {
		$.ajax({
			type : "get",
			url : "book.xml",
			dataType:"xml",
			data : {
				"keyword" : keyword,
				"start" : start,
				"searchType":searchType
			},
			success : function(data) {
				var temp = Handlebars.compile($("#temp").html());
				$(data).find("item").each(function(){
					var image=$(this).find("image").text();
					var author=$(this).find("author").text();
					var price=$(this).find("price").text();
					var description=$(this).find("description").text();
					//var description=$(this).find("콘솔에 찍히는 데이터값").text();
					
					var data=[{image:image,author:author,price:price,description:description}];
					//var data=[{template 안에 data값:function(data)값,author:author,price:price,des:description}];
					$("#container").append(temp(data));
				});
				
			}
		});
	}
	$("#btnSearch").on("click", function() {
		$("#container").html("");
		keyword = $("#keyword").val();

		getList();
	});

	$("#keyword").keydown(function(key) {

		if (key.keyCode == 13) {
			$("#container").html("");
			keyword = $("#keyword").val();
			getList();
		}
	});
	//라이트 박스
	$("#container").on("click",".box img",function(){
		var image=$(this).attr("src");
		var author=$(this).attr("author");
		var price=$(this).attr("price");
		var description=$(this).attr("des");
		$("#image").attr("src", image);
		$("#price").html(price);
		$("#description").html(description);
		$("#author").html(author);
		//alert("안뇽");
		$("#darken-background").show();
	});
	$("#btnclose").on("click", function(){
		$("#darken-background").hide();
	});

 

 

blog.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


	네이버 블로그 검색[네이버 블로그 검색]   
	{{#each items}}
	<tr>
		<td><a href="{{link}}">{{{title}}}</a></td>
	</tr>
	{{/each}}
	 더보기
	var keyword=$("#keyword").val();
	var start=1;
	getList();
	$("#btnmore").on("click",function(){
		start=start+5;
		getList();
		
	});
	function getList(){
		$.ajax({
			type:"get",
			url:"blog.json",
			data:{"keyword":keyword,"start":start},
			success:function(data){
				var temp=Handlebars.compile($("#temp").html());
				$("#tbl").append(temp(data));
			}
		});
	}
	
	$("#btnSearch").on("click", function(){
		$("#tbl").html("");
		keyword=$("#keyword").val();
		
		getList();
	});
	
	$("#keyword").keydown(function(key){
	
		if(key.keyCode==13){
			$("#tbl").html("");
			keyword=$("#keyword").val();
			getList();
		}
	});
	

 

 

book.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>


	네이버 도서 검색[네이버 도서 검색]   

	{{#each items}}
	<div class="box">
		<img src="{{image}}"author="{{author}}" price="{{price}}" des="{{description}}" width=90 height=120>
		<div class="author" author="{{author}}">저자:{{{author}}}</div>
		<div class="price">가격:{{{price}}}원</div>
	</div>
	{{/each}}
	 더보기 


닫기
	var keyword = $("#keyword").val();
	var start = 1;
	getList();
	$("#btnmore").on("click", function() {
		start = start + 5;
		getList();

	});
	function getList() {
		$.ajax({
			type : "get",
			url : "book.json",
			data : {
				"keyword" : keyword,
				"start" : start
			},
			success : function(data) {
				var temp = Handlebars.compile($("#temp").html());
				$("#container").append(temp(data));
			}
		});
	}
	$("#btnSearch").on("click", function() {
		$("#container").html("");
		keyword = $("#keyword").val();

		getList();
	});

	$("#keyword").keydown(function(key) {

		if (key.keyCode == 13) {
			$("#container").html("");
			keyword = $("#keyword").val();
			getList();
		}
	});
	//라이트 박스
	$("#container").on("click",".box img",function(){
		var image=$(this).attr("src");
		var author=$(this).attr("author");
		var price=$(this).attr("price");
		var description=$(this).attr("des");
		$("#image").attr("src", image);
		$("#price").html(price);
		$("#description").html(description);
		$("#author").html(author);
		//alert("안뇽");
		$("#darken-background").show();
	});
	$("#btnclose").on("click", function(){
		$("#darken-background").hide();
	});
	

 

 

movie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>	


	네이버 영화 상세 검색[네이버 영화 상세 검색] 검색:전쟁코미디액션뮤지컬판타지  

	{{#each .}}
	<div class="box">
		<img src="{{image}}"actor="{{actor}}" director="{{director}}" width=90 height=120>
		<div class="actor" actor="{{actor}}">배우{{{actor}}}</div>
		<div class="director">감독{{{director}}}</div>
		<div class="title">제목{{{title}}}</div>
	</div>
	{{/each}}
	 더보기 


닫기

	var searchType=$("#searchType").val()
	var keyword = $("#keyword").val();
	var genre=$("#genre").val(19);
	var start = 1;
	getList();
	$("#btnmore").on("click", function() {
		start = start + 5;
		getList();

	});
	function getList() {
		genre=$("#genre").val();
		$.ajax({
			type : "get",
			url : "movie.xml",
			dataType:"xml",
			data : {
				"keyword" : keyword,
				"start" : start,
				"genre":genre	
			},
			success : function(data) {
				var temp = Handlebars.compile($("#temp").html());
				$(data).find("item").each(function(){
					var image=$(this).find("image").text();
					var actor=$(this).find("actor").text();
					var director=$(this).find("director").text();
					var title=$(this).find("title").text();
					//var description=$(this).find("콘솔에 찍히는 데이터값").text();
					
					var data=[{image:image,actor:actor,director:director,title:title}];
					//var data=[{template 안에 data값:function(data)값,author:author,price:price,des:description}];
					$("#container").append(temp(data));
				});
				
			}
		});
	}
	
	$("#btnSearch").on("click", function() {
		
		$("#container").html("");
		
		keyword = $("#keyword").val();
		genre=$("#genre").val();
		
		getList();
	});

	$("#keyword").keydown(function(key) {

		if (key.keyCode == 13) {
			$("#container").html("");
			keyword = $("#keyword").val();
			genre=$("#genre").val();
			
			getList();
		}
	});
		
		
		
		
	//라이트 박스
	$("#container").on("click",".box img",function(){
		var image=$(this).attr("src");
		var actor=$(this).attr("actor");
		var title=$(this).attr("title");
		var director=$(this).attr("director");
		$("#image").attr("src", image);
		$("#actor").html(actor);
		$("#tltle").html(title);
		$("#director").html(director);
		//alert("안뇽");
		$("#darken-background").show();
	});
	
	$("#btnclose").on("click", function(){
		$("#darken-background").hide();
	});