반응형
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 | import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.List; import javax.swing.JOptionPane; import org.apache.http.client.ClientProtocolException; import com.google.gson.Gson; import study.java.helper.FileHelper; import study.java.helper.GsonHelper; import study.java.helper.HttpHelper; import study.java.model.KakaoBeans; import study.java.model.KakaoBeans.Documents; /** * @file_name : Main.java * @author : 오태현 * @description : 책 제목을 입력 받아 카카오 책 검색 API를 통해 검색결과를 csv형식 파일로 저장 * @last_update : 2018-12-18 */ public class Main { public static void main(String[] args) throws ClientProtocolException, IOException { String bookTitle = JOptionPane.showInputDialog("책 제목을 입력하세요."); String filePath = System.getProperty("user.home") + "\\Desktop\\책 [" + bookTitle + "] 검색결과.csv"; InputStream is = HttpHelper.getInstance().getWebData("https://dapi.kakao.com/v3/search/book?target=title&query=" + URLEncoder.encode(bookTitle, "UTF-8") + "&size=50", "UTF-8"); if (is == null) { System.out.println("데이터 다운로드 실패"); } // GsonHelper로 데이터를 String으로 변환 String isString = GsonHelper.getInstance().getGSONObject(is, "UTF-8"); // Gson 객체 생성 Gson gson = new Gson(); // Beans를 이용해 json 접근 KakaoBeans kb = gson.fromJson(isString, KakaoBeans.class); // 바로 배열이 나오기때문에 List로 해당 값에 접근 List<Documents> documents = kb.getDocuments(); String line = "책 이름,출판사,정가,판매가\n"; for (int i = 0; i < documents.size(); i++) { Documents item = documents.get(i); line += item.getSale_price() == -1 ? item.toString() + "품절\n" : item.toString() + item.getSale_price() + "\n"; } FileHelper.getInstance().writeString(filePath, line, "EUC-KR"); } } | cs |
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 | package study.java.model; import java.util.List; import com.google.gson.annotations.SerializedName; /** * @file_name : KakaoBeans.java * @author : 오태현 * @description : 책 제목, 출판사, 정가, 판매가를 기준으로하는 Beans 설정 * @last_update : 2018-12-18 */ public class KakaoBeans { @SerializedName("documents") private List<Documents> documents; public List<Documents> getDocuments() { return documents; } public void setDocuments(List<Documents> documents) { this.documents = documents; } @Override public String toString() { return "" + documents; } public class Documents { @SerializedName("title") private String title; @SerializedName("publisher") private String publisher; @SerializedName("price") private int price; @SerializedName("sale_price") private int sale_price; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getSale_price() { return sale_price; } public void setSale_price(int sale_price) { this.sale_price = sale_price; } @Override public String toString() { return title.replace(",", " ") + "," + publisher + "," + price + ","; } } } | cs |
반응형
'코딩 예제' 카테고리의 다른 글
Calendar (0) | 2018.12.14 |
---|---|
영화 진흥원 BoxOffice (0) | 2018.12.14 |
Random값의 범위 조절 (0) | 2018.12.12 |
로또번호 (0) | 2018.12.12 |
Kakao link label (0) | 2018.12.09 |