Skip to content

Commit a047a58

Browse files
committed
feat: Retrofit, API 통신 초기 설정
1 parent a7f4b51 commit a047a58

File tree

15 files changed

+159
-109
lines changed

15 files changed

+159
-109
lines changed

.idea/gradle.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.Properties
2+
13
plugins {
24
alias(libs.plugins.android.application)
35
alias(libs.plugins.kotlin.android)
@@ -18,6 +20,14 @@ android {
1820

1921
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2022

23+
// defaultConfig 블록 안에 추가
24+
val properties = Properties()
25+
val localPropertiesFile = rootProject.file("local.properties")
26+
if (localPropertiesFile.exists()) {
27+
properties.load(localPropertiesFile.inputStream())
28+
}
29+
val baseUrl = properties.getProperty("BASE_URL") ?: "http://10.0.2.2:8080/"
30+
buildConfigField("String", "BASE_URL", "\"$baseUrl\"")
2131
}
2232

2333
buildTypes {
@@ -38,6 +48,7 @@ android {
3848
}
3949
buildFeatures {
4050
compose = true
51+
buildConfig = true
4152
}
4253
}
4354

@@ -74,4 +85,9 @@ dependencies {
7485
androidTestImplementation(libs.androidx.espresso.core)
7586
debugImplementation(libs.androidx.compose.ui.tooling)
7687
debugImplementation(libs.androidx.compose.ui.test.manifest)
88+
89+
implementation(libs.retrofit)
90+
implementation(libs.retrofit.converter.gson)
91+
implementation(libs.okhttp)
92+
implementation(libs.okhttp.logging.interceptor)
7793
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.kuit6_android_api.data.api
2+
3+
import com.example.kuit6_android_api.data.model.request.PostCreateRequest
4+
import com.example.kuit6_android_api.data.model.response.BaseResponse
5+
import com.example.kuit6_android_api.data.model.response.PostResponse
6+
import retrofit2.http.Body
7+
import retrofit2.http.GET
8+
import retrofit2.http.POST
9+
import retrofit2.http.Query
10+
11+
12+
interface ApiService {
13+
@GET(value = "/api/posts")
14+
suspend fun getPosts(): BaseResponse<List<PostResponse>>
15+
16+
@POST(value="/api/posts")
17+
suspend fun createPost(
18+
@Query(value = "author") author: String = "일혁",
19+
@Body request: PostCreateRequest
20+
): BaseResponse<PostResponse>
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.kuit6_android_api.data.api
2+
3+
4+
import com.example.kuit6_android_api.BuildConfig
5+
import okhttp3.OkHttpClient
6+
import okhttp3.logging.HttpLoggingInterceptor
7+
import retrofit2.Retrofit
8+
import retrofit2.converter.gson.GsonConverterFactory
9+
import java.util.concurrent.TimeUnit
10+
11+
object RetrofitClient {
12+
private val loggingInterceptor = HttpLoggingInterceptor().apply {
13+
level = HttpLoggingInterceptor.Level.BODY
14+
}
15+
16+
private val okHttpClient = OkHttpClient.Builder()
17+
.addInterceptor(loggingInterceptor)
18+
.connectTimeout(30, TimeUnit.SECONDS)
19+
.readTimeout(30, TimeUnit.SECONDS)
20+
.writeTimeout(30, TimeUnit.SECONDS)
21+
.build()
22+
23+
private val retrofit: Retrofit = Retrofit.Builder()
24+
.baseUrl(BuildConfig.BASE_URL)
25+
.client(okHttpClient)
26+
.addConverterFactory(GsonConverterFactory.create())
27+
.build()
28+
29+
val apiService: ApiService = retrofit.create(ApiService::class.java)
30+
}

app/src/main/java/com/example/kuit6_android_api/data/model/Post.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.kuit6_android_api.data.model.request
2+
3+
import kotlinx.serialization.SerialName
4+
5+
data class PostCreateRequest(
6+
@SerialName(value = "title") val title: String,
7+
@SerialName(value = "content") val content: String,
8+
@SerialName(value = "imageUrl") val imageUrl: String?
9+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import kotlinx.serialization.SerialName
4+
5+
data class AuthorResponse(
6+
@SerialName(value="id") val id: Long,
7+
@SerialName(value="username") val username: String,
8+
@SerialName(value="profileImageUrl") val profileImageUrl: String?
9+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import kotlinx.serialization.SerialName
4+
5+
data class BaseResponse<T>(
6+
@SerialName(value = "success") val success: Boolean,
7+
@SerialName(value = "message") val message: String?,
8+
@SerialName(value = "data") val data: T?,
9+
@SerialName(value = "timestamp") val timestamp: String
10+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class PostResponse(
6+
@SerializedName(value="id") val id:Long,
7+
@SerializedName(value="title") val title:String,
8+
@SerializedName(value="content") val content:String,
9+
@SerializedName(value="imageUrl") val imageUrl:String?,
10+
@SerializedName(value="author") val author: AuthorResponse,
11+
@SerializedName(value="createdAt") val createdAt:String,
12+
@SerializedName(value="updatedAt") val updatedAt:String
13+
)

0 commit comments

Comments
 (0)