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

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - JSON ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑํ•˜๋Š” ๋ฐฉ๋ฒ•(Volley) ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - JSON ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑํ•˜๋Š” ๋ฐฉ๋ฒ•(Volley)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 2. 13:04
๋ฐ˜์‘ํ˜•

Volley ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

MainActivity.java

1. queue ์ƒ์„ฑ

๋„คํŠธ์›Œํฌ ํ†ต์‹ ์— ํ•„์š”ํ•œ queue๋ฅผ onCreate์— ์ƒ์„ฑ

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

 

2. Request๋ฅผ ๋งŒ๋“ ๋‹ค.

์ด ๋•Œ, Response์˜ ํ˜•ํƒœ๋ฅผ ํ™•์ธํ•œ๋‹ค.

[JSON ์šฉ์–ด ์ •๋ฆฌ]
json Array = python List
json (Array)object = python Dict

ํ™•์ธ ํ›„ ๋‹ค์„ฏ๊ฐ€์ง€์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

        JsonArrayRequest request = new JsonArrayRequest(
                Request.Method.GET,
                "https://jsonplaceholder.typicode.com/posts",
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

 

3. queue์— request ์ถ”๊ฐ€

queue์— request  ์ถ”๊ฐ€ํ•˜์—ฌ ์‹คํ–‰ํ•œ๋‹ค.

queue.add(request);
๋ฐ˜์‘ํ˜•