본문 바로가기
Programming/Java

never search api 적용하기 - 자바

by Chan_찬 2016. 9. 2.
728x90
반응형

Never Search API 적용하기

  1. 테스트 코드 / 실행 코드

     
    package com.naver.api.service;
    import org.apache.catalina.util.URLEncoder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    import javax.transaction.Transactional;
    import java.io.UnsupportedEncodingException;
    @Service
    @Transactional
    public class NaverApiService {
        Logger logger = LoggerFactory.getLogger(NaverApiService.class);
        public String getData(String query) {
            String uri = "https://openapi.naver.com/v1/search/blog.xml";
            String result = "";
            try{
                uri += "?query=" + query;
                NaverParse naverAPI = new NaverParse();
                result = naverAPI.parse(uri);
                logger.info("uri:{}", uri);
                logger.info("xml:{}", result);
                logger.info("json:{}", naverAPI.xml2json(result));
            }catch(Exception e){
                e.printStackTrace();
            }
            return result;
        }
    }
     
    package com.naver.api.service;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.XML;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    import javax.transaction.Transactional;
    import javax.xml.parsers.DocumentBuilderFactory;
    import java.io.IOException;
    @Transactional
    public class NaverParse {
        protected String parse(String url){
            String clientId = "client_id";//본인발급내용
            String clientSecret = "client_secret";//본인발급내용
            String xml = "";
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(url);
            request.addHeader("X-Naver-Client-Id", clientId);
            request.addHeader("X-Naver-Client-Secret", clientSecret);
            try {
                HttpResponse response = client.execute(request);
                xml = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return xml;
        }
        protected static String xml2json(String xml){
            int PRETTY_PRINT_INDENT_FACTOR = 4;
            String json = "";
            JSONObject xmlJSONObj = null;
            try {
                xmlJSONObj = XML.toJSONObject(xml);
                json = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return json;
        }
    }
    package com.naver.api.service;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import static org.junit.Assert.*;
    public class NaverApiServiceTest {
        @Test
        public void testGetData() throws Exception {
            NaverApiService ns = new NaverApiService();
            String query = "tesla";
            ns.getData(query);
            query = "누진제";
            ns.getData(query);
        }
    }


728x90
728x90
BIG
Buy me a coffeeBuy me a coffee

댓글