본문 바로가기

안드로이드 스튜디오

[안드로이드 스튜디오]네트워크 통신: Volley 라이브러리 사용법 및 예제

안드로이드 개발에서 네트워크 통신은 필수적인 요소이다. Volley 라이브러리는 네트워크 요청을 쉽게 처리할 수 있는 강력한 도구다. 이번 포스트에서는 Volley 라이브러리를 사용하여 네트워크 요청을 보내고 응답을 처리하는 방법을 알아보려한다.

 

https://google.github.io/volley/

 

Volley overview

Volley overview Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network

google.github.io

 

 

 

1. Volley 라이브러리 설정

먼저, 프로젝트의 build.gradle 파일에 Volley 라이브러리를 추가한다.

dependencies {
    implementation 'com.android.volley:volley:1.2.1'
}

2. AndroidManifest.xml 파일 설정

네트워크 통신을 위해 인터넷 권한을 추가한다.

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

3. 네트워크 요청 구현

네트워크 요청을 보내기 위해 RequestQueue를 생성하고, JsonObjectRequest를 사용하여 데이터를 요청한다.

 

MainActivity.java

package com.kks.employeeinfo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.kks.employeeinfo.adapter.EmployerAdapter;
import com.kks.employeeinfo.model.Employer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Button btnAdd;
    ProgressBar progressBar;

    RecyclerView recyclerView;
    ArrayList<Employer> employerArrayList = new ArrayList<>();
    EmployerAdapter adapter;

    public ActivityResultLauncher<Intent> launcher =
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                    new ActivityResultCallback<ActivityResult>() {
                        @Override
                        public void onActivityResult(ActivityResult o) {
                            Log.i("LIFE MAIN", "onActivityResult 실행");
                            if(o.getResultCode() == 1000){
                                Employer employer = (Employer) o.getData().getSerializableExtra("employer");
                                employerArrayList.add(0, employer);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAdd = findViewById(R.id.btnAdd);
        progressBar = findViewById(R.id.progressBar);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                launcher.launch(intent);
            }
        });

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.GET,
                "https://block1-image-test.s3.ap-northeast-2.amazonaws.com/employees.json",
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progressBar.setVisibility(View.GONE);
                        Log.i("EMPLOYER MAIN", response.toString());

                        try {
                            String status = response.getString("status");
                            if (!status.equals("success")) {
                                Toast.makeText(MainActivity.this, "네트워크 에러", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            JSONArray data = response.getJSONArray("data");
                            for (int i = 0; i < data.length(); i++) {
                                JSONObject row = data.getJSONObject(i);
                                int id = row.getInt("id");
                                String name = row.getString("employee_name");
                                int salary = row.getInt("employee_salary");
                                int age = row.getInt("employee_age");

                                Employer employer = new Employer(id, name, salary, age);
                                employerArrayList.add(employer);
                            }

                            adapter = new EmployerAdapter(MainActivity.this, employerArrayList);
                            recyclerView.setAdapter(adapter);

                        } catch (JSONException e) {
                            Toast.makeText(MainActivity.this, "파싱 에러", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, "네트워크 통신 에러", Toast.LENGTH_SHORT).show();
                        Log.i("EMPLOYER MAIN", error.toString());
                    }
                });

        queue.add(request);
    }
}

4. Adapter 클래스 작성

EmployerAdapter.java

package com.kks.employeeinfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.kks.employeeinfo.MainActivity;
import com.kks.employeeinfo.R;
import com.kks.employeeinfo.UpdateActivity;
import com.kks.employeeinfo.model.Employer;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

public class EmployerAdapter extends RecyclerView.Adapter<EmployerAdapter.ViewHolder>{

    Context context;
    ArrayList<Employer> employerArrayList;

    public EmployerAdapter(Context context, ArrayList<Employer> employerArrayList) {
        this.context = context;
        this.employerArrayList = employerArrayList;
    }

    @NonNull
    @Override
    public EmployerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.employer_row, parent, false);
        return new EmployerAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Employer employer = employerArrayList.get(position);

        holder.txtName.setText(employer.name);
        holder.txtAge.setText(employer.age + "세");

        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
        holder.txtSalary.setText(numberFormat.format(employer.salary));
    }

    @Override
    public int getItemCount() {
        return employerArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtName;
        TextView txtAge;
        TextView txtSalary;
        ImageView imgDelete;
        CardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtName = itemView.findViewById(R.id.txtName);
            txtAge = itemView.findViewById(R.id.txtAge);
            txtSalary = itemView.findViewById(R.id.txtSalary);
            imgDelete = itemView.findViewById(R.id.imgDelete);
            cardView = itemView.findViewById(R.id.cardView);

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = getAdapterPosition();
                    if (index != RecyclerView.NO_POSITION) {
                        Employer employer = employerArrayList.get(index);
                        Intent intent = new Intent(context, UpdateActivity.class);
                        intent.putExtra("employer", employer);
                        intent.putExtra("index", index);

                        if (context instanceof MainActivity) {
                            ((MainActivity) context).launcher.launch(intent);
                        }
                    }
                }
            });
        }
    }
}

5. 데이터 모델 클래스 작성

Employer.java

package com.kks.employeeinfo.model;

import java.io.Serializable;

public class Employer implements Serializable {
    public int id;
    public String name;
    public int salary;
    public int age;

    public Employer() {}

    public Employer(int id, String name, int salary, int age) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.age = age;
    }
}

6. AddActivity.java 작성

AddActivity.java

package com.kks.employeeinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;
import com.kks.employeeinfo.model.Employer;

public class AddActivity extends AppCompatActivity {
    EditText editName;
    EditText editSalary;
    EditText editAge;
    Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        editName = findViewById(R.id.editName);
        editSalary = findViewById(R.id.editSalary);
        editAge = findViewById(R.id.editAge);
        btnSave = findViewById(R.id.btnSave);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editName.getText().toString().trim();
                String strSalary = editSalary.getText().toString().trim();
                String strAge = editAge.getText().toString().trim();

                if (name.isEmpty() || strSalary.isEmpty() || strAge.isEmpty()) {
                    Snackbar.make(btnSave, "필수항목 모두 입력하세요", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                int salary = Integer.parseInt(strSalary);
                int age = Integer.parseInt(strAge);

                Employer employer = new Employer(1000, name, salary, age);
                Intent intent = new Intent();
                intent.putExtra("employer", employer);
                setResult(1000, intent);

                Toast.makeText(AddActivity.this, "저장되었습니다", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}

 

실행 결과