๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Android] POST ๋ฐฉ์‹ ํ†ต์‹  (2023.07.19~20) ๋ณธ๋ฌธ

full stack/์•ˆ๋“œ๋กœ์ด๋“œ

[Android] POST ๋ฐฉ์‹ ํ†ต์‹  (2023.07.19~20)

Shinjio 2023. 7. 19. 14:06

ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ ์‹ค์Šต 


github ์ฐธ์กฐ

node, android code

 

https://github.com/sin6338ki/Nodejs/tree/master/Ex13Android

https://github.com/sin6338ki/android/tree/main/AndServer

 

 

 


 

object : 

๊ตฌํ˜„์ฒด๋ฅผ 1ํšŒ์„ฑ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ 

 

 

//object: -> ๋ฌด๋ช…๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ‚ค์›Œ๋“œ
            //  StringRequest๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ๋‹ค!
            //  ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ๋ถˆ๊ฐ€ -> ๊ตฌํ˜„์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ
            //  ๊ทธ๋Ÿฐ๋ฐ Bํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜๋งŒ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ์—๋Š” ๋น„ํšจ์œจ์ ์ž„
            //  ์ด๋•Œ object ์‚ฌ์šฉ > ํ•œ๋ฒˆ๋งŒ ์“ธ ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ!
            val request = object:StringRequest(

                Request.Method.POST,
                "", //http ์š”์ฒญ
                {
                    response ->
                    Log.d("response", response.toString())
                },
                {
                    error ->
                    Log.d("error", error.toString())
                }

            ){
                //post : ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์ด ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„
                //                              ๋ฐ˜ํ™˜ํƒ€์ž…
                override fun getParams() : MutableMap<String, AndMemberVO>{
                    val params:MutableMap<String, AndMemberVO> = HashMap<String, AndMemberVO>()
                    //MutableMap์€ interface๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ๋ถˆ๊ฐ€
                    val amv = AndMemberVO(inputId, inputPw, inputTel, inputBirth)
                    params.put("AndMember", amv) //ํšŒ์›์„ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์„œ ๋ณด๋ƒ„ (memberVO)
                    return params
                }

 

 


 

Mutable Map 

 

 

gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜์—ฌ json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ ํ›„ ์ „์†ก

 

https://github.com/google/gson

 

GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back

A Java serialization/deserialization library to convert Java Objects into JSON and back - GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back

github.com

 

override fun getParams() : MutableMap<String, String>{
    //Map : ์ •์ , MutableMap : ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ Map
    val params:MutableMap<String, String> = HashMap<String, String>()
    
    //MutableMap์€ interface๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ๋ถˆ๊ฐ€
    val amv = AndMemberVO(inputId, inputPw, inputTel, inputBirth)
    
    //VO ์ž์ฒด๋Š” ํƒ€ ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ์–ด๋ ค์›€ -> JSONํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ›„ ๋ณด๋‚ด์•ผ ํ•จ
    //AndMemberVO(Object)-> JSON ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
    //GSON(goole์—์„œ ๋งŒ๋“  json์„ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ์‚ฌ์šฉ
    params.put("AndMember", Gson().toJson(amv)) //ํšŒ์›์„ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์„œ ๋ณด๋ƒ„ (memberVO)
    return params
}

 

Node์—์„œ ๋ฐ›์„ ๋•Œ๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฐ›์•„์ง → JSON์œผ๋กœ ๋ณ€ํ™˜

 

 

์œ„ : ๋ฌธ์ž์—ด ํ˜•ํƒœ, ์•„๋ž˜ : JSONํ˜•ํƒœ

 


 

http ํ†ต์‹ ์„ ์œ„ํ•œ ๋ฐฉ๋ฒ•

 

android:usesCleartextTraffic="true"

 

 

 

 

๋ฌธ์ž์—ด๋กœ ๋“ค์–ด์˜ด => jsonํ˜•ํƒœ๋กœ ๋ฐ›๊ธฐ

 

JSON.parse(req.body.AndMember)