λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’š 개발/μ›Ή

[μ•ˆλ“œλ‘œμ΄λ“œ] AsyncTask 와 HttpURLConnection ...(2) Web Sample Code (jsp)

by devshin.kr 2021. 9. 14.
728x90

 

json-simple-1.1.1.jar
0.02MB

2021.08.26 - [개발/μ•ˆλ“œλ‘œμ΄λ“œ] - [μ•ˆλ“œλ‘œμ΄λ“œ] AsyncTask 와 HttpURLConnection ...(1) Android Sample Code

 

[μ•ˆλ“œλ‘œμ΄λ“œ] AsyncTask 와 HttpURLConnection ...(1) Android Sample Code

AsyncTask λž€ 비동기 μž‘μ—…μ„ ν•  수 μžˆλ„λ‘ μ œκ³΅ν•˜λŠ” ν΄λž˜μŠ€μ΄λ‹€. 비동기λ₯Ό μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ”, λ™κΈ°μ‹μœΌλ‘œ μž‘μ—…μ„ ν•˜κ²Œ 되면 main thread μ—μ„œ A thread 둜 κ°€μ„œ μž‘μ—…μ„ ν•˜λ‹€κ°€.. A thread κ°€ λλ‚˜μ•Όλ§Œ λ‹€μ‹œ mai

devshin93.tistory.com

 

1. μ•ˆλ“œλ‘œμ΄λ“œμ™€ HttpURLConnection ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν•˜κΈ° μœ„ν•΄ μ›Ή ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν–ˆλ‹€.

(μ•„λž˜ 글을 μ°Έκ³ ν•΄μ„œ μƒˆλ‘œμš΄ ν”„λ‘œμ νŠΈλ₯Ό 생성)

2021.05.02 - [개발/μ•ˆλ“œλ‘œμ΄λ“œ] - [μ•ˆλ“œλ‘œμ΄λ“œ] μ›Ήλ·° 디버깅 μ‰½κ²Œ ν•˜λŠ” 방법 - Chrome inspect μ‚¬μš©λ²• (1)

 

[μ•ˆλ“œλ‘œμ΄λ“œ] μ›Ήλ·° 디버깅 μ‰½κ²Œ ν•˜λŠ” 방법 - Chrome inspect μ‚¬μš©λ²• (1)

μ›Ήλ·°λŠ” λͺ¨λ°”일 λ‹¨λ§κΈ°μ—μ„œ μ›ΉνŽ˜μ΄μ§€λ₯Ό λ„μš°κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€. Chrome inspect λ₯Ό μ‚¬μš©ν•˜λ©΄ μ•ˆλ“œλ‘œμ΄λ“œ μ›Ήλ·°μ—μ„œλ„ μ›ΉλΈŒλΌμš°μ €μ—μ„œ F12λ₯Ό 눌러 λ””λ²„κΉ…ν•˜λŠ” 것과 같은 효과λ₯Ό λ‚Ό 수 μžˆλ‹€. λ¨Όμ € μƒˆλ‘œμš΄

devshin93.tistory.com

 

 

2. JSONObject, JSONArray, JSONParser 클래슀λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄ λ‹€μŒ 라이브러리λ₯Ό μΆ”κ°€ν–ˆλ‹€.

WebContent > WEB-INF > lib > json-simple-1.1.1.jar

 

3. μƒ˜ν”Œ μ½”λ“œ μž‘μ„± (test.jsp)

<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.stream.Collectors"%>
<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.parser.JSONParser"%>
<%
	String sJsonObj = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
	JSONParser parser = new JSONParser();
	
	System.out.println(sJsonObj);
	
	Object obj = parser.parse(sJsonObj);
	JSONObject jsonObj = (JSONObject)obj;
	String input1 = (String) jsonObj.get("input1");
	String input2 = (String) jsonObj.get("input2");
	
	System.out.println(input1);
	System.out.println(input2);
	
	String retData = "Server to Client";
	JSONObject jObject = new JSONObject();
	
	try {
		jObject.put("nRv", "0");
		jObject.put("sRet", retData);
	} catch(Exception e) {
		jObject.put("nRv", "1");
		e.printStackTrace();
	}
	
	out.println(jObject.toString());
%>

 

 

4. κ²°κ³Ό ν™”λ©΄

μ•ˆλ“œλ‘œμ΄λ“œ κ²°κ³Ό ν™”λ©΄

 

 

μ›Ή μ„œλ²„ κ²°κ³Ό ν™”λ©΄

λŒ“κΈ€