Commit de53c8cc authored by Deokhyun Lee's avatar Deokhyun Lee

added key value

parent 23c89f48
Pipeline #2101 failed with stages
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.ldk.util.Util;
public class NaverAPI {
String clientId = APICode.ID;
String clientSecret = APICode.PWD;
public String searchNews(String str) {
String apiURL = "https://openapi.naver.com/v1/search/news?query=" + str; // json 결과
//String apiURL = "https://openapi.naver.com/v1/search/blog.xml?query="+ text; // xml 결과
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("X-Naver-Client-Id", clientId);
requestHeaders.put("X-Naver-Client-Secret", clientSecret);
String responseBody = get(apiURL,requestHeaders);
return responseBody;
}
public String searchBlog(String str) {
String apiURL = "https://openapi.naver.com/v1/search/blog?query=" + str; // json 결과
//String apiURL = "https://openapi.naver.com/v1/search/blog.xml?query="+ text; // xml 결과
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("X-Naver-Client-Id", clientId);
requestHeaders.put("X-Naver-Client-Secret", clientSecret);
String responseBody = get(apiURL,requestHeaders);
return responseBody;
}
public String get(String apiUrl, Map<String, String> requestHeaders) {
HttpURLConnection con = connect(apiUrl);
try {
con.setRequestMethod("GET");
for (Map.Entry<String, String> header : requestHeaders.entrySet()) {
con.setRequestProperty(header.getKey(), header.getValue());
}
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // 정상 호출
return readBody(con.getInputStream());
} else { // 에러 발생
return readBody(con.getErrorStream());
}
} catch (IOException e) {
throw new RuntimeException("API 요청과 응답 실패", e);
} finally {
con.disconnect();
}
}
public HttpURLConnection connect(String apiUrl) {
try {
URL url = new URL(apiUrl);
return (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
throw new RuntimeException("API URL이 잘못되었습니다. : " + apiUrl, e);
} catch (IOException e) {
throw new RuntimeException("연결이 실패했습니다. : " + apiUrl, e);
}
}
public String readBody(InputStream body) {
InputStreamReader streamReader = new InputStreamReader(body);
try (BufferedReader lineReader = new BufferedReader(streamReader)) {
StringBuilder responseBody = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
responseBody.append(line);
}
return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException("API 응답을 읽는데 실패했습니다.", e);
}
}
}
package com.ldk.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Util {
// 오늘 날짜를 가져 오는 메소드(yyyy-MM-dd)
public static String getCurrentDate(String fmt) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
return sdf.format(new Date());
}
// 파일 읽어서 화면에 출력(파일 내용을 여기 메소드 밖으로 보내고 싶다)
public static String readLineFile(String filePath) {
BufferedReader br = null;
String retLine = "";
// StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new FileReader(filePath));
while (true) {
String line = br.readLine();
if (line == null)
break;
retLine += line + "\n"; // 문자열을 리턴
// sb.append(line + "\n");
// System.out.println(line); // 화면에 출력
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return retLine; // sb.toString()
// return sb.toString();
}
// 파일 쓰기(새파일로 쓰기, 덮어쓰기)
public static void writeLineFile(ArrayList<String> strList, String filePath) {
PrintWriter out = null;
try {
out = new PrintWriter(filePath);
for(String s : strList) {
out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}
// 파일 쓰기(이어 쓰기)
public static void writeLineFile(ArrayList<String> strList, String filePath, boolean isAppend) {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(filePath, isAppend));
for(String s : strList) {
out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}
// 내가 만든 문자열을 잘라서 데이터 전달
public static void mySplit(String str, String regex1, String regex2) {
String[] strArr = str.split(regex1);
for (String s : strArr) {
// System.out.println(s);
String[] tmpArr = s.split(regex2);
// for (String ss : tmpArr) {
// System.out.println(ss);
// }
// System.out.println(tmpArr.length);
for (int i = 0; i < tmpArr.length; i++) {
if (i == tmpArr.length-1) {
System.out.print(tmpArr[i]);
} else {
System.out.print(tmpArr[i] + " / ");
}
}
System.out.println();
}
}
}
package dto;
public class NaverApiDTO {
// 제목
private String title;
// 링크
private String link;
// 날짜
private String pubDate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment