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

Welcome! Everything is fine.

[Andorid] Volley ์‚ฌ์šฉ๋ฒ• ๋ณธ๋ฌธ

Android

[Andorid] Volley ์‚ฌ์šฉ๋ฒ•

๊ฐœ๋ฐœ๊ณฐ๋ฐœ 2022. 5. 14.
728x90

๐Ÿ“Œ Volley๋ž€?

์›น ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ์š”์ฒญ(Request) ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์š”์ฒญ ํ(RequestQueue)์— ๋„ฃ์–ด์ฃผ๋ฉด ์š”์ฒญ ํ๊ฐ€ ์•Œ์•„์„œ ์›น ์„œ๋ฒ„์— ์š”์ฒญํ•˜๊ณ  ์‘๋‹ต๊นŒ์ง€ ๋ฐ›์•„์ค€๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋‘๋ฉด ์‘๋‹ต์ด ์™”์„ ๋•Œ ๊ทธ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค. Volley ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์žฅ์ ์€ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์ด๋‹ค.

๐Ÿ“Œ Volley ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๊ธฐ

Volley๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฏ€๋กœ build.gradle ํŒŒ์ผ์— ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Gradle Scripts > build.gradle(Module: ~) ์—์„œ dependencies์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

implementation 'com.android.volley:volley:1.2.1'

๐Ÿ“Œ INTERNET ๊ถŒํ•œ ์ถ”๊ฐ€ํ•˜๊ธฐ

์ธํ„ฐ๋„ท์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— AndroidManifest.xml ํŒŒ์ผ์— INTERNET ๊ถŒํ•œ์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค. ๋˜ application ํƒœ๊ทธ์— useCleartextTraffic์†์„ฑ์„ true๋กœ ์ค€๋‹ค.

<uses-permission android:name="android.permission.INTERNET"/>
android:usesCleartextTraffic="true"

๐Ÿ“Œ ์›น ์š”์ฒญํ•˜๊ธฐ

์ž…๋ ฅ์ƒ์ž์— ์‚ฌ์ดํŠธ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์š”์ฒญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์›น ์š”์ฒญ์„ ํ•˜๋„๋ก MainActivity.java ํŒŒ์ผ์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ๋จผ์ € OnCreate() ๋ฉ”์„œ๋“œ ์•ˆ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ํ™”๋ฉด์— ๋งŒ๋“  ์š”์ฒญํ•˜๊ธฐ ๋ฒ„ํŠผ(requestBtn)์„ ๋ˆ„๋ฅผ ์‹œ makeRequest() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์š”์ฒญ ํ๋Š” static ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์‹ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ ํ•œ ํ›„ ํ• ๋‹นํ–ˆ๋‹ค. ์š”์ฒญ ํ๋Š” ํ•œ ๋ฒˆ๋งŒ ๋งŒ๋“ค์–ด ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์š”์ฒญ ํ๋Š” ํ•˜๋‚˜์˜ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•ฑ ์ „์ฒด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์š”์ฒญ ํ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” Volley.newRequestQueue ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    private ActivityMainBinding binding;
    static RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.requestBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                makeRequest(); // ์š”์ฒญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด makeRequest() ๋ฉ”์„œ๋“œ ์‹คํ–‰ํ•จ.
            }
        });

        if(requestQueue == null) {
            requestQueue = Volley.newRequestQueue(getApplicationContext()); // ์š”์ฒญ ํ๋ฅผ ๋งŒ๋“ฆ.
        }
    }

makeRequest() ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด StringRequest ํด๋ž˜์Šค๋กœ ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. StringRequest๋Š” ๋ฌธ์ž์—ด์„ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์š”์ฒญ ๊ฐ์ฒด์ด๊ณ , ์ด์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์š”์ฒญ ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”๋ฐ, ์ผ๋ฐ˜์ ์œผ๋กœ StringRequest ๊ฐ์ฒด๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํ•˜๋‹ค. ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ฃผ์„์— ์ ์–ด๋†“์•˜๋‹ค.

package com.example.samplerequest;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.samplerequest.databinding.ActivityMainBinding;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    static RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.requestBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                makeRequest(); // ์š”์ฒญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด makeRequest() ๋ฉ”์„œ๋“œ ์‹คํ–‰ํ•จ.
            }
        });

        if(requestQueue == null) {
            requestQueue = Volley.newRequestQueue(getApplicationContext()); // ์š”์ฒญ ํ๋ฅผ ๋งŒ๋“ฆ.
        }
    }

    public void makeRequest() {
        String url = binding.editText.getText().toString(); // ๋ณ€์ˆ˜ url์— ์ž…๋ ฅ์ƒ์ž์— ๋“ค์–ด๊ฐ„ ์ฃผ์†Œ๋ฅผ String์œผ๋กœ ๋ฐ”๊ฟ” ๋„ฃ์Œ.

        // ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ฆ.(StringRequest - ๋ฌธ์ž์—ด์„ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์š”์ฒญ๊ฐ์ฒด)
        /* ์š”์ฒญ๊ฐ์ฒด๋ฅผ new ์—ฐ์‚ฐ์ž๋กœ ๋งŒ๋“ค ๋•Œ ์ด 4๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ
        1. GET or POST ๋ฉ”์„œ๋“œ ์ „๋‹ฌ(์š”์ฒญ ๋ฐฉ์‹ ์ง€์ •)
        2. url(์›น ์‚ฌ์ดํŠธ ์ฃผ์†Œ) ์ „๋‹ฌ
        3. ์‘๋‹ต๋ฐ›์„ ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด ์ „๋‹ฌ(์‘๋‹ต ๋ฐ›์œผ๋ฉด onResponse ๋ฉ”์„œ๋“œ ์ž๋™ ํ˜ธ์ถœ)
        4. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋  ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด ์ „๋‹ฌ
         */
        StringRequest request = new StringRequest(Request.Method.GET, url, 
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        println("์‘๋‹ต -> " + response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        println("์—๋Ÿฌ -> " + error.getMessage());
                    }
                }
                ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();

                return params;
            }
        };
        request.setShouldCache(false);
        requestQueue.add(request); // ์š”์ฒญ ํ์˜ add ๋ฉ”์„œ๋“œ๋กœ ์š”์ฒญ ๊ฐ์ฒด ๋„ฃ๊ธฐ
        println("์š”์ฒญ ๋ณด๋ƒ„.");
    }

    public void println(String data) {
        binding.requestTv.append(data + "\n");
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ ์˜คํ”ˆ API์˜ Request URL๋ฅผ ๋„ฃ๊ณ  ์š”์ฒญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋”๋‹ˆ JSON ํ˜•ํƒœ๋กœ ๋œ ์‘๋‹ต ๊ฒฐ๊ณผ๊ฐ€ ํ…์ŠคํŠธ๋ทฐ์— ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.