[Android Studio] Volley, Gson ๋ผ์ด๋ธ๋ฌ๋ฆฌ JSON Data ํ์ฑ -> recylerView list ๋์์ฃผ๊ธฐ ++gif๋ฃ๊ธฐ
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 ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ๋ถ์ํด๋ณด์.
ํฌ๋กฌ ํ์ฅํ๋ก๊ทธ๋จ ์ถ๊ฐํ๋ฉด 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&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์ ๋ณํํ์ฌ ๋๊ฒจ์ค๋ค.
+ ์์งค๋ฃ๊ธฐ