Skip to content

Commit

Permalink
[fix] 재난 문자 여러개 가져오기 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-naa committed Jan 10, 2024
1 parent 36efd20 commit 7bb8e48
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/disaster")
Expand All @@ -24,7 +25,7 @@ public MessageController(MessageService messageService) {
}

@GetMapping("/message")
public MessageResponse getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {
public List<MessageResponse> getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {

return messageService.message(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -29,14 +30,14 @@ public class MessageService {

private final NaverTransService naverTransService;

public MessageResponse message(User user) throws IOException, ParseException {
public List<MessageResponse> message(User user) throws IOException, ParseException {
StringBuilder urlBuilder = new StringBuilder("https://www.safetydata.go.kr/openApi");

urlBuilder.append("/" + URLEncoder.encode("행정안전부_긴급재난문자","UTF-8"));
urlBuilder.append("?serviceKey=" + secretKey);
urlBuilder.append("&returnType=json");
urlBuilder.append("&pageNum=1");
urlBuilder.append("&numRowsPerPage=1");
urlBuilder.append("&numRowsPerPage=4");

URL url = new URL(urlBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Expand Down Expand Up @@ -69,9 +70,41 @@ public MessageResponse message(User user) throws IOException, ParseException {
JSONObject jsonObject = (JSONObject) jsonParser.parse(responseBody);

JSONObject responseData = (JSONObject) jsonObject.get("responseData");
JSONArray data = (JSONArray) responseData.get("data");
JSONArray dataArr = (JSONArray) responseData.get("data");

JSONObject dataObject = (JSONObject) data.get(0);
List<MessageResponse> msgResArr = new ArrayList<>();

for(int i=0; i<dataArr.size(); i++){
JSONObject dataObject = (JSONObject) dataArr.get(i);

String CREAT_DT = dataObject.get("CREAT_DT").toString();
String DSSTR_SE_ID = dataObject.get("DSSTR_SE_ID").toString();
String DSSTR_SE_NM = dataObject.get("DSSTR_SE_NM").toString();
String EMRGNCY_STEP_ID = dataObject.get("EMRGNCY_STEP_ID").toString();
String MSG_CN = dataObject.get("MSG_CN").toString();
String RCV_AREA_ID = dataObject.get("RCV_AREA_ID").toString();
String RCV_AREA_NM = dataObject.get("RCV_AREA_NM").toString();

List<String> areaIdArr = Arrays.stream(RCV_AREA_ID.split(",")).toList();
for(int j=0;j<areaIdArr.size();j++){
String a = areaIdArr.get(j);
}

List<String> areaNmArr = Arrays.stream(RCV_AREA_NM.split(",")).toList();
for(int j=0;j<areaNmArr.size();j++){
String a = areaNmArr.get(j);
}

String translatedMSG = naverTransService.getTransSentence(MSG_CN, user);
MessageResponse response = new MessageResponse(MSG_CN, translatedMSG, CREAT_DT, areaIdArr, areaNmArr, EMRGNCY_STEP_ID, DSSTR_SE_ID, DSSTR_SE_NM);

msgResArr.add(response);
}

return msgResArr;

/*
JSONObject dataObject = (JSONObject) dataArr.get(0);
String CREAT_DT = dataObject.get("CREAT_DT").toString();
String DSSTR_SE_ID = dataObject.get("DSSTR_SE_ID").toString();
Expand All @@ -94,6 +127,7 @@ public MessageResponse message(User user) throws IOException, ParseException {
String translatedMSG = naverTransService.getTransSentence(MSG_CN, user);
MessageResponse response = new MessageResponse(MSG_CN, translatedMSG, CREAT_DT, areaIdArr, areaNmArr, EMRGNCY_STEP_ID, DSSTR_SE_ID, DSSTR_SE_NM);
return response;
*/
}
}

0 comments on commit 7bb8e48

Please sign in to comment.