크루메이트 코드 리뷰!!
식단관리 - 칼로리 사전
행복하게사는게꿈
2020. 9. 18. 03:03
데이터 전처리 후 데이터베이스 삽입
@RequestMapping("searchAjax")
public String insertMenu(String a, String b, Map<String, String> params) throws Exception {
StringBuilder urlBuilder = new StringBuilder(
""); /* URL */
URL url = new URL(urlBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
System.out.println("Response code: " + conn.getResponseCode());
BufferedReader rd;
if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
System.out.println(sb.toString());
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(sb.toString());
// JSONObject xmlJSONObj = XML.toJSONObject(sb.toString());
JSONObject I2790JSON = (JSONObject) jsonObject.get("I2790");
JSONArray rowJSONArray = (JSONArray) I2790JSON.get("row");
String menu_kcal = "";
String menu_name = "";
for (int i = 0; i < rowJSONArray.size(); i++) {
System.out.println("=row_" + i + " ===========================================");
// 배열 안에 있는것도 JSON형식 이기 때문에 JSON Object 로 추출
JSONObject rowObject = (JSONObject) rowJSONArray.get(i);
// JSON name으로 추출
System.out.println("rowInfo: NUTR_CONT3==>" + rowObject.get("NUTR_CONT3"));
System.out.println("rowInfo: NUTR_CONT2==>" + rowObject.get("NUTR_CONT2"));
System.out.println("rowInfo: NUTR_CONT1==>" + rowObject.get("NUTR_CONT1"));
System.out.println("rowInfo: SERVING_SIZE==>" + rowObject.get("SERVING_SIZE"));
System.out.println("rowInfo: DESC_KOR==>" + rowObject.get("DESC_KOR"));
menu_kcal = rowObject.get("NUTR_CONT1").toString();
menu_name = rowObject.get("DESC_KOR").toString();
if (rowObject.get("NUTR_CONT1").toString().equals("")) {
menu_kcal = "0";
}
params.put("menu_kcal", menu_kcal);
params.put("menu_name", menu_name);
dietService.insertMenu(params);
}
// NUTR_CONT1 열량
// DESC_KOR 식품이름
// String xmlJSONObjString = xmlJSONObj.toString();
System.out.println("아");
return "success";
}