μλλ‘μ΄λ MVVM ν¨ν΄μ μ μ©ν Retrofit 2 + OkHttp 5 + RxJava 3 νλ‘μ νΈλ₯Ό ꡬμ±ν©λλ€.
bash
볡μ¬νΈμ§
app/
βββ data/
β βββ model/ # API λ°μ΄ν° λͺ¨λΈ (Post.java)
β βββ network/ # Retrofit API μΈν°νμ΄μ€ λ° ν΄λΌμ΄μΈνΈ (ApiService.java, RetrofitClient.java)
β βββ repository/ # λ°μ΄ν° λ μ΄μ΄ (PostRepository.java)
βββ ui/
β βββ view/ # UI κ΄λ ¨ ν΄λμ€ (MainActivity.java, PostAdapter.java)
β βββ viewmodel/ # ViewModel (PostViewModel.java)
build.gradle (Module: app)Retrofit 2, OkHttp 5, RxJava 3, LiveData λ° ViewModel κ΄λ ¨ λΌμ΄λΈλ¬λ¦¬λ₯Ό μΆκ°ν©λλ€.
gradle
볡μ¬νΈμ§
dependencies {
// κΈ°λ³Έ λΌμ΄λΈλ¬λ¦¬
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.recyclerview:recyclerview:1.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.6.2'
implementation 'androidx.lifecycle:lifecycle-livedata:2.6.2'
// Retrofit 2 + OkHttp 5
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp5:okhttp:5.0.0-alpha.11'
implementation 'com.squareup.okhttp5:logging-interceptor:5.0.0-alpha.11'
// RxJava 3 + Retrofit Adapter
implementation 'io.reactivex.rxjava3:rxjava:3.1.6'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
}
Post.java)Retrofitμ΄ API μλ΅(JSON)μ μλμΌλ‘ Java κ°μ²΄λ‘ λ³νν μ μλλ‘ λ°μ΄ν° λͺ¨λΈμ μμ±ν©λλ€.
java
볡μ¬νΈμ§
package com.example.retrofitexample.data.model;
import com.google.gson.annotations.SerializedName;
public class Post {
@SerializedName("userId")
private int userId;
@SerializedName("id")
private int id;
@SerializedName("title")
private String title;
@SerializedName("body")
private String body;
public int getUserId() { return userId; }
public int getId() { return id; }
public String getTitle() { return title; }
public String getBody() { return body; }
}
ApiService.java)Retrofitμμ μ¬μ©ν API μμ²μ μ μν©λλ€.
java
볡μ¬νΈμ§
package com.example.retrofitexample.data.network;
import com.example.retrofitexample.data.model.Post;
import java.util.List;
import io.reactivex.rxjava3.core.Single;
import retrofit2.http.GET;
public interface ApiService {
@GET("posts")
Single<List<Post>> getPosts();
}
RetrofitClient.java)Retrofitκ³Ό OkHttp 5λ₯Ό μ€μ ν©λλ€.
java
볡μ¬νΈμ§
package com.example.retrofitexample.data.network;
import java.util.concurrent.TimeUnit;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("<https://jsonplaceholder.typicode.com/>")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.io()))
.build();
}
return retrofit;
}
}