Retrofit ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์๋๋ก์ด๋์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- Retrofit์ Square์์ ๊ฐ๋ฐํ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
- ์ฃผ๋ก Android ์ ํ๋ฆฌ์ผ์ด์ ์์ RESTful ์น ์๋น์ค๋ฅผ ํธ์ถํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
- ์ฆ, REST API์ ์ํธ์์ฉํ๊ธฐ ์ฝ๊ฒ ๋์์ค๋ค.
- JSON์ Java ๊ฐ์ฒด๋ก ๋ณํํ๊ฑฐ๋ ๊ทธ ๋ฐ๋๋ก ๋ณํํ๋ ์์ ์ ๊ฐ๋จํ๊ฒ ์ํํ ์ ์๋ค.
- Retrofit ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ JSON์ ์๋ฐ์ ํด๋์ค๋ก ๋ฐ๋ก ๋ด์์ฃผ๋ฏ๋ก ํ์ฑ์ ํ ํ์๊ฐ ์์ด์ ๊ฐํธํ๋ค.
- Retrofit์ ์ฌ์ฉํ๋ฉด ๋คํธ์ํฌ ์์ฒญ์ ์ฝ๊ฒ ๋ง๋ค๊ณ ๊ด๋ฆฌํ ์ ์๋ค.
Retrofit (1.x)
- Retrofit 1.x๋ HTTP ํด๋ผ์ด์ธํธ๋ก Apache HTTPClient๋ฅผ ์ฌ์ฉํ๋ค
- ๊ธฐ๋ณธ์ ์ผ๋ก Call ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ์ธํฐํ์ด์ค ๋ฉ์๋์ ๋ฐํ ํ์ ์ผ๋ก ์ง์ ์ ์ธ POJO ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค
- RxJava์์ ํตํฉ์ ๋ณ๋์ ์ด๋ํฐ๊ฐ ์๋ ๋ฐฉ์์ผ๋ก ์ ๊ณต๋๋ค
- Gson์ ๊ธฐ๋ณธ์ ์ผ๋ก ํตํฉ๋์ด ์์์ผ๋ฉฐ, ์ถ๊ฐ์ ์ธ ์ค์ ์์ด ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ค
Retrofit2
- Retrofit 2.x๋ HTTP ํด๋ผ์ด์ธํธ๋ก OkHttp๋ฅผ ์ฌ์ฉํ๋ค ์ด๋ ๋คํธ์ํฌ ์์ฒญ ๋ฐ ์๋ต์ ์ฒ๋ฆฌ๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ํ๋ค
- ๋ฐํ ํ์ : Call<T> ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝ๋์๋ค ์ด๋ ๋คํธ์ํฌ ์์ฒญ์ด ๋น๋๊ธฐ์ ์ผ๋ก ์ํ๋ ์ ์๋๋ก ๋์์ค๋ค
Call<MyResponse> call = api.getMyData();
- ์ด๋ํฐ : ๋ค์ํ ํธ์ถ ์ด๋ํฐ(Call Adapters)๋ฅผ ์ง์ํ๋ค. ์๋ฅผ ๋ค์ด, RxJava๋ Kotlin Coroutines์ ์ฝ๊ฒ ํตํฉํ ์ ์๋๋ก ๋ณ๋์ ์ด๋ํฐ๋ฅผ ์ ๊ณตํ๋ค
@GET("users/{user}") Observable<User> getUser(@Path("user") String user);
- Gson ํตํฉ : Gson์ ์ฌ์ ํ ์ฌ์ฉํ ์ ์์ง๋ง, ๋ช ์์ ์ผ๋ก Retrofit ๊ฐ์ฒด์ ์ถ๊ฐํด์ผ ํ๋ค
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com") .addConverterFactory(GsonConverterFactory.create()) .build();
https://github.com/square/retrofit
ํ๊ฒฝ๋ณ์ ์ค์ ํ๊ธฐ
Retrofit ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์นํ๊ธฐ
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
Sync๋ฅผ ๋๋ฌ์ฃผ๋ฉด ์ค์น๊ฐ ์๋ฃ๋๋ค.
Retrofit ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ฝ๋ ์ง๊ธฐ ์ ์ ๋ด์ผํ ์ ํ๋ธ ๋ฐ์ดํฐ API ๋ง๋๋ ๋ฒ
https://codebunny99.tistory.com/163
๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
Config ๋ง๋ค๊ธฐ
package com.~.youtube2.config;
public class Config {
public static final String YOUTUBE_KEY = "์์ ์ key ์
๋ ฅ";
public static final String DOMAIN = "https://www.googleapis.com";
}
NetworkClient ํด๋์ค ์์ฑํ๊ธฐ
package com.~.youtube2.api;
import android.content.Context;
import com.~.youtube2.config.Config;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NetworkClient {
public static Retrofit retrofit;
public static Retrofit getRetrofitClient(Context context){
if(retrofit == null){
// ํต์ ๋ก๊ทธ ํ์ธํ ๋ ํ์ํ ์ฝ๋
HttpLoggingInterceptor loggingInterceptor =
new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// ๋คํธ์ํฌ ์ฐ๊ฒฐ๊ด๋ จ ์ฝ๋
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
.addInterceptor(loggingInterceptor)
.build();
// ๋คํธ์ํฌ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ณ ๋ฐ๋
// ๋ ํธ๋กํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ด๋ จ ์ฝ๋
retrofit = new Retrofit.Builder()
.baseUrl(Config.DOMAIN)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
VideoApi ์ธํฐํ์ด์ค ์์ฑํ๊ธฐ
package com.~.youtube2.api;
import com.~.youtube2.model.VideoList;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface VideoApi { //์ธํฐํ์ด์ค๋ก ๋ฐ๊ฟ์ค๋ค.
// ์ ํ๋ธ์ ๊ฒ์ํ๋ API
// HTTP Method ์จ์ฃผ๊ณ , ๊ทธ ์์๋ ๊ฒฝ๋ก๋ฅผ ์จ์ค๋ค.
@GET("/youtube/v3/search")
Call<VideoList> searchVideo(@Query("key") String key,
@Query("part") String part,
@Query("maxResults") int maxResults,
@Query("order") String order,
@Query("type") String type,
@Query("q") String q );
// ํจ์์ ๋ฆฌํด ๋ฐ์ดํฐ ํ์
์, Call ์์ ์๋ต์ผ๋ก ๋ฐ์ ํด๋์ค๋ฅผ ๋ฃ์ด์ค๋ค.
}
model ํจํค์ง์ VideoList.class ์์ฑํ๊ธฐ
{ } ๋ก ๋์ด์๋ ์ค๋ธ์ ํธ๋ ํด๋์ค๋ก ์์ฑํ๋ค.
package com.~.youtube2.model;
import java.util.ArrayList;
public class VideoList {
public String nextPageToken;
public ArrayList<Item> items;
}
model ํจํค์ง์ ํด๋์ค ๋ง๋ค๊ธฐ
ImageUrl ํด๋์ค
package com.~.youtube2.model;
public class ImageUrl {
public String url;
}
Item ํด๋์ค
package com.~.youtube2.model;
public class Item {
public Id id;
public Video snippet;
}
Video ํด๋์ค
package com.~.youtube2.model;
public class Video {
public String title;
public String description;
public Thumbnail thumbnails;
}
Thumbnail ํด๋์ค
package com.yujinoh.youtube2.model;
public class Thumbnail {
public ImageUrl medium;
public ImageUrl high;
}
Id ํด๋์ค
package com.~.youtube2.model;
public class Id {
public String videoId;
}
activity_main.xml ์์ฑํ๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/editSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="5"
android:ems="10"
android:hint="๊ฒ์์ด ์
๋ ฅ..."
android:inputType="text"
android:textSize="22sp" />
<ImageView
android:id="@+id/imgSearch"
android:layout_width="45dp"
android:layout_height="45dp"
app:srcCompat="@drawable/search_24dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topLayout" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_row.xml ๋ง๋ค์ด์ฃผ๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="TextView"
android:textSize="24sp" />
<TextView
android:id="@+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="10dp"
android:ellipsize="end"
android:maxLines="2"
android:text="TextView"
android:textSize="20sp" />
<ImageView
android:id="@+id/imgThumb"
android:layout_width="320dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:src="@drawable/image_24dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
MainActivity ์์ฑํ๊ธฐ
package com.~.youtube2;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.~.youtube2.adapter.VideoAdapter;
import com.~.youtube2.api.NetworkClient;
import com.~.youtube2.api.VideoApi;
import com.~.youtube2.config.Config;
import com.~.youtube2.model.Item;
import com.~.youtube2.model.VideoList;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {
EditText editSearch;
ImageView imgSearch;
ProgressBar progressBar;
RecyclerView recyclerView;
ArrayList<Item> videoArrayList = new ArrayList<>();
VideoAdapter adapter;
String keyword;
String nextPageToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editSearch = findViewById(R.id.editSearch);
imgSearch = findViewById(R.id.imgSearch);
progressBar = findViewById(R.id.progressBar);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
//ํ๋ก๊ทธ๋ ์ค๋ฐ ์๋ณด์ด๊ฒ ํ๊ธฐ
progressBar.setVisibility(View.GONE);
imgSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
keyword = editSearch.getText().toString().trim();
if (keyword.isEmpty()) {
Toast.makeText(MainActivity.this, "๊ฒ์์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", Toast.LENGTH_SHORT).show();
return;
}
// ๋คํธ์ํฌ๋ก API๋ฅผ ํธ์ถํ๋ค
getNetworkData();
}
});
}
private void getNetworkData() {
//ํ๋ก๊ทธ๋ ์ค๋ฐ ๋ณด์ด๊ฒ ํ๊ธฐ
progressBar.setVisibility(View.VISIBLE);
Retrofit retrofit = NetworkClient.getRetrofitClient(MainActivity.this);
VideoApi api = retrofit.create(VideoApi.class);
// API๋ฅผ ๋ง๋ค๊ณ
Call<VideoList> call =api.searchVideo(Config.YOUTUBE_KEY, "snippet", 20, "date", "video", keyword);
// ๋คํธ์ํฌ๋ก ํธ์ถํ๋ค.
call.enqueue(new Callback<VideoList>() {
@Override
public void onResponse(Call<VideoList> call, Response<VideoList> response) {
// ๋คํธ์ํฌ ํต์ ์ฑ๊ณต
// ํ๋ก๊ทธ๋ ์ค๋ฐ ์๋ณด์ด๊ฒ ํ๊ธฐ
progressBar.setVisibility(View.GONE);
// 200 OK ์ผ๋ ์ฝ๋
if(response.isSuccessful()){
// ์๋ตํ๋ ์ ์ฒด JSON ๋ฐ์ดํฐ๋ body์ ๋ค์ด์์ผ๋ฏ๋ก body() ํจ์ ํธ์ถํ๋ค.
// Retrofit ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ JSON์ ์๋ฐ์ ํด๋์ค๋ก ๋ฐ๋ก ๋ด์์ฃผ๋ฏ๋ก ํ์ฑํ ํ์๊ฐ ์๋ค.
VideoList videoList = response.body();
nextPageToken = videoList.nextPageToken;
videoArrayList.addAll( videoList.items );
adapter = new VideoAdapter(MainActivity.this, videoArrayList);
recyclerView.setAdapter(adapter);
}else if(response.code() == 400){
// response.code()๋ HTTP ์ํ์ฝ๋๊ฐ ๋ค์ด์๋ค.
}else if(response.code() == 500){
}else {
}
}
@Override
public void onFailure(Call<VideoList> call, Throwable throwable) {
// ๋คํธ์ํฌ ํต์ ์คํจ
// ํ๋ก๊ทธ๋ ์ค๋ฐ ์๋ณด์ด๊ฒ ํ๊ธฐ
progressBar.setVisibility(View.GONE);
// ์ ์ ํํ
์๋ ค์ฃผ๊ณ , ๋ก๊ทธ์ฐ๊ณ ๋ฆฌํด
}
});
}
}
Adapter ๋ง๋ค๊ธฐ
package com.~.youtube2.adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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.~.youtube2.R;
import com.~.youtube2.model.Item;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder>{
Context context;
ArrayList<Item> videoArrayList;
public VideoAdapter(Context context, ArrayList<Item> videoArrayList) {
this.context = context;
this.videoArrayList = videoArrayList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Item video = videoArrayList.get(position);
holder.txtTitle.setText( video.snippet.title );
holder.txtDescription.setText( video.snippet.description );
Glide.with(context).load( video.snippet.thumbnails.medium.url ).into( holder.imgThumb );
}
@Override
public int getItemCount() {
return videoArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
TextView txtDescription;
ImageView imgThumb;
CardView cardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtDescription = itemView.findViewById(R.id.txtDescription);
imgThumb = itemView.findViewById(R.id.imgThumb);
cardView = itemView.findViewById(R.id.cardView);
}
// ์น๋ธ๋ผ์ฐ์ ์กํฐ๋นํฐ๋ฅผ ์คํ์ํค๋ ํจ์
void openWebPage(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
}
}