Android/Android Studio

[Android Studio] Volley, Gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ JSON Data ํŒŒ์‹ฑ -> recylerView list ๋„์›Œ์ฃผ๊ธฐ ++gif๋„ฃ๊ธฐ

MK_____ 2021. 11. 9. 15:58
package com.example.volly;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {

    //๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Volly๋ฅผ ์”€์œผ๋กœ์จ ๋‚ด๋ถ€์ ์œผ๋กœ ์“ฐ๋ ˆ๋“œ, ํ•ธ๋“ค๋Ÿฌ๊นŒ์ง€ ๋‹ค ์ฒ˜๋ฆฌํ•ด์คŒ

    EditText editText;
    TextView textView;

    static RequestQueue requestQueue;

    //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
    RecyclerView recyclerView;
    MovieAdapter adapter;

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

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
        recyclerView = findViewById(R.id.recylerView);
        //๋ ˆ์ด์•„์›ƒ ์–ด๋–ป๊ฒŒ ํ• ์ง€, listํ˜• or gridํ˜•
//        LinearLayoutManager layoutManager = new LinearLayoutManager
//                (this, LinearLayoutManager.VERTICAL, true);
//        recyclerView.setLayoutManager(layoutManager);


        adapter = new MovieAdapter();
        recyclerView.setAdapter(adapter);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String urlStr = editText.getText().toString();
                request(urlStr);
            }
        });

        requestQueue = Volley.newRequestQueue(getApplicationContext());


    }

    public void request(String urlStr){
        //Volly๋Š” ์š”์ฒญ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  Que์•ˆ์— ๋„ฃ์œผ๋ฉด ui์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‘๋‹ต์„ return ํ•ด์คŒ!
        StringRequest request = new StringRequest(
                Request.Method.GET,
                urlStr, // urlStr์„ GET๋ฐฉ์‹์œผ๋กœ ๋ณด๋ƒˆ์„ ๋•Œ ์ •์ƒ์ ์œผ๋กœ Respose๊ฐ€ ์˜ค๋ฉด ๋ฐ›์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ
                new Response.Listener<String>() {
                    @Override //์ •์ƒ ์‘๋‹ต
                    public void onResponse(String response) {
                        println("์‘๋‹ต -> " + response); //์‘๋‹ต๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๋ฉด JSON ํฌ๋งท์œผ๋กœ ๋˜์–ด ์žˆ์Œ!

                        //์ด JSON ์‘๋‹ต๊ฐ’์„ ๋„ฃ์–ด์ค„ class๋ฅผ ์ •์˜ํ•ด์คฌ๊ณ  (Movie, MovieList, MovieListResult)
                        //์ฒ˜๋ฆฌํ•ด์ค„๊ฑฐ์•ผ
                        processRespons(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override //์—๋Ÿฌ ์‘๋‹ต
                    public void onErrorResponse(VolleyError error) {
                        println("์—๋Ÿฌ -> " + error.toString());
                    }
                }
        ){
            @Override //๋งŒ๋“ค์–ด์ง„ ์š”์ฒญ ๊ฐ์ฒด
            protected Map<String, String> getParams() throws AuthFailureError {
               Map<String, String> params = new HashMap<String, String >();

               return params; //์ด ์š”์ฒญ๊ฐ์ฒด๋ฅผ que์•ˆ์— ๋„ฃ์œผ๋Ÿฌ ์ถœ๋ฐœ (์œ„๋กœ)
            }
        };

        request.setShouldCache(false);
        requestQueue.add(request);
        println("์š”์ฒญ ๋ณด๋ƒ„");
    }

    //์‘๋‹ต ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ
    public void processRespons(String response){
        Gson gson = new Gson();

        //JSON๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ class๋กœ ์ •์˜ํ•ด์ค€ ๊ฐ์ฒด๋กœ ๋‹ด์•„์ค€๋‹ค
        MovieList movieList = gson.fromJson(response, MovieList.class);
        println("์˜ํ™” ์ •๋ณด์˜ ์ˆ˜ : " + movieList.boxOfficeResult.dailyBoxOfficeList.size());

        //recyclerView์— ๋ฆฌ์ŠคํŠธ๋กœ ๋ณด์—ฌ์ค„ ํฌ๋ฌธ ์ถ”๊ฐ€~!
        for (int i = 0; i < movieList.boxOfficeResult.dailyBoxOfficeList.size(); i++){
            Movie movie = movieList.boxOfficeResult.dailyBoxOfficeList.get(i);
            adapter.addItem(movie);
        }

        adapter.notifyDataSetChanged();

    }

    //์‘๋‹ต์ด๋‚˜ ์—๋Ÿฌ๊ฐ€ ์˜ค๋ฉด ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ ์ •์˜
    public void println(String data){
        textView.append(data + "\n");
    }


}โ€‹
package com.example.volly;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {

    //๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Volly๋ฅผ ์”€์œผ๋กœ์จ ๋‚ด๋ถ€์ ์œผ๋กœ ์“ฐ๋ ˆ๋“œ, ํ•ธ๋“ค๋Ÿฌ๊นŒ์ง€ ๋‹ค ์ฒ˜๋ฆฌํ•ด์คŒ

    EditText editText;
    TextView textView;

    static RequestQueue requestQueue;

    //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
    RecyclerView recyclerView;
    MovieAdapter adapter;

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

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
        recyclerView = findViewById(R.id.recylerView);
        //๋ ˆ์ด์•„์›ƒ ์–ด๋–ป๊ฒŒ ํ• ์ง€, listํ˜• or gridํ˜•
//        LinearLayoutManager layoutManager = new LinearLayoutManager
//                (this, LinearLayoutManager.VERTICAL, true);
//        recyclerView.setLayoutManager(layoutManager);


        adapter = new MovieAdapter();
        recyclerView.setAdapter(adapter);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String urlStr = editText.getText().toString();
                request(urlStr);
            }
        });

        requestQueue = Volley.newRequestQueue(getApplicationContext());


    }

    public void request(String urlStr){
        //Volly๋Š” ์š”์ฒญ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  Que์•ˆ์— ๋„ฃ์œผ๋ฉด ui์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‘๋‹ต์„ return ํ•ด์คŒ!
        StringRequest request = new StringRequest(
                Request.Method.GET,
                urlStr, // urlStr์„ GET๋ฐฉ์‹์œผ๋กœ ๋ณด๋ƒˆ์„ ๋•Œ ์ •์ƒ์ ์œผ๋กœ Respose๊ฐ€ ์˜ค๋ฉด ๋ฐ›์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ
                new Response.Listener<String>() {
                    @Override //์ •์ƒ ์‘๋‹ต
                    public void onResponse(String response) {
                        println("์‘๋‹ต -> " + response); //์‘๋‹ต๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๋ฉด JSON ํฌ๋งท์œผ๋กœ ๋˜์–ด ์žˆ์Œ!

                        //์ด JSON ์‘๋‹ต๊ฐ’์„ ๋„ฃ์–ด์ค„ class๋ฅผ ์ •์˜ํ•ด์คฌ๊ณ  (Movie, MovieList, MovieListResult)
                        //์ฒ˜๋ฆฌํ•ด์ค„๊ฑฐ์•ผ
                        processRespons(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override //์—๋Ÿฌ ์‘๋‹ต
                    public void onErrorResponse(VolleyError error) {
                        println("์—๋Ÿฌ -> " + error.toString());
                    }
                }
        ){
            @Override //๋งŒ๋“ค์–ด์ง„ ์š”์ฒญ ๊ฐ์ฒด
            protected Map<String, String> getParams() throws AuthFailureError {
               Map<String, String> params = new HashMap<String, String >();

               return params; //์ด ์š”์ฒญ๊ฐ์ฒด๋ฅผ que์•ˆ์— ๋„ฃ์œผ๋Ÿฌ ์ถœ๋ฐœ (์œ„๋กœ)
            }
        };

        request.setShouldCache(false);
        requestQueue.add(request);
        println("์š”์ฒญ ๋ณด๋ƒ„");
    }

    //์‘๋‹ต ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ
    public void processRespons(String response){
        Gson gson = new Gson();

        //JSON๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ class๋กœ ์ •์˜ํ•ด์ค€ ๊ฐ์ฒด๋กœ ๋‹ด์•„์ค€๋‹ค
        MovieList movieList = gson.fromJson(response, MovieList.class);
        println("์˜ํ™” ์ •๋ณด์˜ ์ˆ˜ : " + movieList.boxOfficeResult.dailyBoxOfficeList.size());

        //recyclerView์— ๋ฆฌ์ŠคํŠธ๋กœ ๋ณด์—ฌ์ค„ ํฌ๋ฌธ ์ถ”๊ฐ€~!
        for (int i = 0; i < movieList.boxOfficeResult.dailyBoxOfficeList.size(); i++){
            Movie movie = movieList.boxOfficeResult.dailyBoxOfficeList.get(i);
            adapter.addItem(movie);
        }

        adapter.notifyDataSetChanged();

    }

    //์‘๋‹ต์ด๋‚˜ ์—๋Ÿฌ๊ฐ€ ์˜ค๋ฉด ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ ์ •์˜
    public void println(String data){
        textView.append(data + "\n");
    }


}

Volley๋ž€?

์•ฑ์—์„œ ์„œ๋ฒ„์™€ http ํ†ต์‹ ์„ ํ•  ๋•Œ HttpURLConnection์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง์ ‘ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๋ฐ›๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ์ง์ ‘ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ธฐ๋ณธ์ ์ธ ์ฝ”๋“œ ์–‘ ๋˜ํ•œ ๋งŽ์•„ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ๋Š” ์‰ฝ๊ณ  ๋น ๋ฅธ http ํ†ต์‹ ์„ ์œ„ํ•ด Volley ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

 

์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค๋Š” ์  ์™ธ์—๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์žฅ์ ๋“ค์ด ์žˆ์–ด http ํ†ต์‹ ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

Volley ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

 

์‚ฌ์šฉ์ž๊ฐ€ Request ๊ฐ์ฒด์— ์š”์ฒญ ๋‚ด์šฉ์„ ๋‹ด์•„ RequestQueue์— ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด,

RequestQueue๊ฐ€ ์•Œ์•„์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ๋ฐ›๋Š”๋‹ค.

์‘๋‹ต์ด ์˜ค๋ฉด RequestQueue์—์„œ Request์— ๋“ฑ๋ก๋œ ResponseListener๋กœ ์‘๋‹ต์„ ์ „๋‹ฌํ•ด์ค€๋‹ค.

๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž๋Š” ๋ณ„๋„์˜ ์“ฐ๋ ˆ๋“œ ๊ด€๋ฆฌ ๋ฟ ์•„๋‹ˆ๋ผ UI ์ ‘๊ทผ์„ ์œ„ํ•œ handler ๋˜ํ•œ ๋‹ค๋ฃฐ ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์‚ฌ์šฉ๋ฒ•

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

Manifest์— uses-permission ํƒœ๊ทธ๋กœ INTERNET ๊ถŒํ•œ์„ ์ถ”๊ฐ€ํ•˜์ž.

//Volley
implementation 'com.android.volley:volley:1.1.1'

//Gson
implementation 'com.google.code.gson:gson:2.8.5'

build.gradle์˜ dependencies์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž.

 

Gson์ด๋ž€?

Json์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋ ค๋ฉด Json ํ˜•์‹์— ๋งž์ถฐ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํ•ฉํ•˜๊ณ , ํ•ด์„ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค.

์ด ๊ณผ์ •์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด์„ ๊ธฐํ˜ธ์— ๋”ฐ๋ผ ๋ถ„๋ฆฌํ•˜๊ณ  ํ•ฉ์น˜๋Š” ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

์ด๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ๊ธ€์—์„œ๋Š” Gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

 

Gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Json ๋ฌธ์ž์—ด์„ ํ•ด์„ํ•˜์—ฌ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ณ , ๊ฐ์ฒด๋ฅผ Json ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๋Š” ์ง์ ‘ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  Json ํ˜•์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Gson ์‚ฌ์šฉ๋ฒ•

์˜ํ™”๊ด€์ž…์žฅ๊ถŒํ†ตํ•ฉ์ „์‚ฐ๋ง open api๋กœ๋ถ€ํ„ฐ sample json ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€ ๋ถ„์„ํ•ด๋ณด์ž.

http://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=f5eef3421c602c6cb7ea224104795888&targetDt=20120101

 

ํฌ๋กฌ ํ™•์žฅํ”„๋กœ๊ทธ๋žจ ์ถ”๊ฐ€ํ•˜๋ฉด JSON Data ์ด์˜๊ฒŒ ๋ณด์—ฌ์คŒ!

์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค

 

๋ณดํ†ต JSON data์˜ ๊ฐ’์„ ํ•„๋“œ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

์ด ๋•Œ key๊ฐ’๊ณผ ๋ฉค๋ฒ„๋ณ€์ˆ˜์˜ ๋ณ€์ˆ˜๋ช…์„ ๋™์ผํ•˜๊ฒŒ ๊ตฌ์„ฑํ•ด์ฃผ์–ด์•ผ Gson์„ ํ†ตํ•ด ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="http://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=f5eef3421c602c6cb7ea224104795888&amp;targetDt=20120101" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="์š”์ฒญ" />

    <!-- ์˜ํ™” list๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ์ถ”๊ฐ€ -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recylerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#7A7A7A"
        android:layout_weight="1"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
	<!-- ์ด๋•Œ RecyclerView ์•ˆ์— textView ๊ฐ™์€ view ์š”์†Œ๋ฅผ ๋„ฃ์–ด์คฌ๋”๋‹ˆ, (์“ฐ์ง€๋„ ์•Š์„๊ฑฐ์ง€๋งŒ)
     	  LayoutManager Error๊ฐ€ ์ž๊พธ ๋–ด๋‹ค. 
          ***RecyclerView์•ˆ์—๋Š” ๋ญ˜ ๋„ฃ์œผ๋ฉด ์•ˆ๋Œ! ๊ทธ๋ž˜์„œ ๋ฐ”๋กœ /> ๋กœ ๋‹ซ์•„์ฃผ๋Š” ๊ฒƒ์ด ๊ฑด๊ฐ•์— ์ข‹๋‹ค! -->      
 

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF3B8"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="textView ์ž๋ฆฌ"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

** xml์ƒ (view์˜์—ญ์—์„œ ์—์„œ ๋ฐ”๋กœ app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค ) ==> MainActivity.java ์†Œ์Šค์ƒ์—์„œ linearLayout์„ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹จ ๋œป! 

 

 

๊ทธ๋ž˜์„œ View์™€ Model ์„ ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Œ! MVVM ์— ํ•œ๋ฐœ์ž๊ตญ~~~โ€ผ๏ธ!

 

 

 

 

 ์ด๋•Œ RecyclerView ์•ˆ์— textView ๊ฐ™์€ view ์š”์†Œ๋ฅผ ๋„ฃ์–ด์คฌ๋”๋‹ˆ, (์“ฐ์ง€๋„ ์•Š์„๊ฑฐ์ง€๋งŒ)
        LayoutManager Error๊ฐ€ ์ž๊พธ ๋–ด๋‹ค. 
          ***RecyclerView์•ˆ์—๋Š” ๋ญ˜ ๋„ฃ์œผ๋ฉด ์•ˆ๋จ! ๊ทธ๋ž˜์„œ ๋ฐ”๋กœ /> ๋กœ ๋‹ซ์•„์ฃผ๋Š” ๊ฒƒ์ด ๊ฑด๊ฐ•์— ์ข‹๋‹ค! 

MainActivity.java

package com.example.volly;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {

    //๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Volly๋ฅผ ์”€์œผ๋กœ์จ ๋‚ด๋ถ€์ ์œผ๋กœ ์“ฐ๋ ˆ๋“œ, ํ•ธ๋“ค๋Ÿฌ๊นŒ์ง€ ๋‹ค ์ฒ˜๋ฆฌํ•ด์คŒ

    EditText editText;
    TextView textView;

    static RequestQueue requestQueue;

    //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
    RecyclerView recyclerView;
    MovieAdapter adapter;

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

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        //recyclerView ์ถ”๊ฐ€๋ถ€๋ถ„
        recyclerView = findViewById(R.id.recylerView);
        //๋ ˆ์ด์•„์›ƒ ์–ด๋–ป๊ฒŒ ํ• ์ง€, listํ˜• or gridํ˜•
//        LinearLayoutManager layoutManager = new LinearLayoutManager
//                (this, LinearLayoutManager.VERTICAL, true);
//        recyclerView.setLayoutManager(layoutManager);


        adapter = new MovieAdapter();
        recyclerView.setAdapter(adapter);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String urlStr = editText.getText().toString();
                request(urlStr);
            }
        });

        requestQueue = Volley.newRequestQueue(getApplicationContext());


    }

    public void request(String urlStr){
        //Volly๋Š” ์š”์ฒญ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  Que์•ˆ์— ๋„ฃ์œผ๋ฉด ui์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‘๋‹ต์„ return ํ•ด์คŒ!
        StringRequest request = new StringRequest(
                Request.Method.GET,
                urlStr, // urlStr์„ GET๋ฐฉ์‹์œผ๋กœ ๋ณด๋ƒˆ์„ ๋•Œ ์ •์ƒ์ ์œผ๋กœ Respose๊ฐ€ ์˜ค๋ฉด ๋ฐ›์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ
                new Response.Listener<String>() {
                    @Override //์ •์ƒ ์‘๋‹ต
                    public void onResponse(String response) {
                        println("์‘๋‹ต -> " + response); //์‘๋‹ต๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๋ฉด JSON ํฌ๋งท์œผ๋กœ ๋˜์–ด ์žˆ์Œ!

                        //์ด JSON ์‘๋‹ต๊ฐ’์„ ๋„ฃ์–ด์ค„ class๋ฅผ ์ •์˜ํ•ด์คฌ๊ณ  (Movie, MovieList, MovieListResult)
                        //์ฒ˜๋ฆฌํ•ด์ค„๊ฑฐ์•ผ
                        processRespons(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override //์—๋Ÿฌ ์‘๋‹ต
                    public void onErrorResponse(VolleyError error) {
                        println("์—๋Ÿฌ -> " + error.toString());
                    }
                }
        ){
            @Override //๋งŒ๋“ค์–ด์ง„ ์š”์ฒญ ๊ฐ์ฒด
            protected Map<String, String> getParams() throws AuthFailureError {
               Map<String, String> params = new HashMap<String, String >();

               return params; //์ด ์š”์ฒญ๊ฐ์ฒด๋ฅผ que์•ˆ์— ๋„ฃ์œผ๋Ÿฌ ์ถœ๋ฐœ (์œ„๋กœ)
            }
        };

        request.setShouldCache(false);
        requestQueue.add(request);
        println("์š”์ฒญ ๋ณด๋ƒ„");
    }

    //์‘๋‹ต ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ
    public void processRespons(String response){
        Gson gson = new Gson();

        //JSON๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ class๋กœ ์ •์˜ํ•ด์ค€ ๊ฐ์ฒด๋กœ ๋‹ด์•„์ค€๋‹ค
        MovieList movieList = gson.fromJson(response, MovieList.class);
        println("์˜ํ™” ์ •๋ณด์˜ ์ˆ˜ : " + movieList.boxOfficeResult.dailyBoxOfficeList.size());

        //recyclerView์— ๋ฆฌ์ŠคํŠธ๋กœ ๋ณด์—ฌ์ค„ ํฌ๋ฌธ ์ถ”๊ฐ€~!
        for (int i = 0; i < movieList.boxOfficeResult.dailyBoxOfficeList.size(); i++){
            Movie movie = movieList.boxOfficeResult.dailyBoxOfficeList.get(i);
            adapter.addItem(movie);
        }

        adapter.notifyDataSetChanged();

    }

    //์‘๋‹ต์ด๋‚˜ ์—๋Ÿฌ๊ฐ€ ์˜ค๋ฉด ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ ์ •์˜
    public void println(String data){
        textView.append(data + "\n");
    }


}

 

MovieAdapter.java

package com.example.volly;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import java.util.ArrayList;

import androidx.recyclerview.widget.RecyclerView;

//recyclerView๋Š” ์„ ํƒ์œ„์ ฏ, ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋Œ‘ํ„ฐ๊ฐ€ ํ•„์š”!
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>{

    ArrayList<Movie> items = new ArrayList<Movie>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.movie_item, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Movie item = items.get(position);
        holder.setItem(item);
    }

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

    public void addItem(Movie item){
        items.add(item);
    }

    public void setItems(ArrayList<Movie> items){
        this.items = items;
    }

    public Movie getItem(int position){
        return items.get(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        TextView movieNm;
        TextView audiCnt;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //View ์•ˆ์—์„œ id๋กœ ์ฐพ์•„์ฃผ๊ธฐ
            movieNm = itemView.findViewById(R.id.movieNm);
            audiCnt = itemView.findViewById(R.id.audiCnt);

        }

        public void setItem(Movie item){
            movieNm.setText(item.movieNm);
            audiCnt.setText(item.audiCnt + " ๋ช…");
        }
    }
}

 

 

์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์ด ๋“ค์–ด์˜ค๋ฉด ์‘๋‹ต์„ ๋ฐ›์•„์™€ parseResponse() ํ•จ์ˆ˜์— ๋„˜๊ฒจ์ค€๋‹ค.

 

parseResponse()์—์„œ๋Š” ๋จผ์ € ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜์„ ์œ„ํ•œ Gson ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  Gson ๊ฐ์ฒด์˜ fromJson() ํ•จ์ˆ˜์— ์‘๋‹ต๋ฐ›์€ String๊ณผ ์ตœ์ƒ์œ„ ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด,

Gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋„˜๊ฒจ์ค€ ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ String์„ ๋ณ€ํ™˜ํ•˜์—ฌ ๋„˜๊ฒจ์ค€๋‹ค.

 

 

+ ์›€์งค๋„ฃ๊ธฐ