From c89f07594196bbc3904ab1045790a82d50a23da7 Mon Sep 17 00:00:00 2001 From: Doyoon Kim Date: Mon, 14 Oct 2024 23:24:55 +0900 Subject: [PATCH] ![FIX] Resolve Unexpected Exception - Resolve unexpected HTTP 404 exception occurred during network request. * Separate Retrofit instance into two individual variables, one for Notice Service, and another one for the FCM service. --- app/src/main/AndroidManifest.xml | 5 +--- .../knutice/data/KnuticeRemoteSource.kt | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0e4d6ad..b7b261a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,7 +15,6 @@ android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/Theme.KNUTICE" - android:directBootAware="true" tools:targetApi="31"> + android:exported="false"> - diff --git a/app/src/main/java/com/doyoonkim/knutice/data/KnuticeRemoteSource.kt b/app/src/main/java/com/doyoonkim/knutice/data/KnuticeRemoteSource.kt index 10020bb..32495a7 100644 --- a/app/src/main/java/com/doyoonkim/knutice/data/KnuticeRemoteSource.kt +++ b/app/src/main/java/com/doyoonkim/knutice/data/KnuticeRemoteSource.kt @@ -7,6 +7,7 @@ import com.doyoonkim.knutice.model.TokenInfo import com.doyoonkim.knutice.model.TopThreeNotices import com.doyoonkim.knutice.model.ValidateTokenResult import com.example.knutice.BuildConfig +import com.google.gson.Gson import com.google.gson.JsonObject import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Deferred @@ -28,20 +29,19 @@ import javax.inject.Inject class KnuticeRemoteSource @Inject constructor() { - private object RetrofitInstance { - private val builder = Retrofit.Builder() - .addConverterFactory(GsonConverterFactory.create()) + private val noticeService = Retrofit.Builder() + .baseUrl(BuildConfig.API_ROOT) + .addConverterFactory(GsonConverterFactory.create()) + .build() - val noticeServiceInstance: Retrofit - get() = builder.baseUrl(BuildConfig.API_ROOT).build() - - val fcmServiceInstance: Retrofit - get() = builder.baseUrl(BuildConfig.API_ROOT_FCM).build() - } + private val fcmService = Retrofit.Builder() + .baseUrl(BuildConfig.API_ROOT_FCM) + .addConverterFactory(GsonConverterFactory.create()) + .build() suspend fun getTopThreeNotice(): TopThreeNotices { Log.d("KnuticeRemoteSource", "Start retrofit service") - return RetrofitInstance.noticeServiceInstance.create(KnuticeService::class.java).run { + return noticeService.create(KnuticeService::class.java).run { this.getTopThreeNotice() } } @@ -49,7 +49,7 @@ class KnuticeRemoteSource @Inject constructor() { suspend fun getNoticeListPerPage(category: NoticeCategory, lastNttId: Int): NoticesPerPage { Log.d("KnuticeRemoteSource", "Start retrofit service") - return RetrofitInstance.noticeServiceInstance.create(KnuticeService::class.java).run { + return noticeService.create(KnuticeService::class.java).run { if (lastNttId == 0) { this.getFirstPageOfNotice(category) } else { @@ -68,9 +68,12 @@ class KnuticeRemoteSource @Inject constructor() { fun validateToken(token: String) { CoroutineScope(Dispatchers.IO).launch { - RetrofitInstance.fcmServiceInstance.create(KnuticeService::class.java).validateToken( + fcmService.create(KnuticeService::class.java).validateToken( TokenInfo(deviceToken = token) - ) + ).run { + if (this.result?.resultCode == 200) Log.d("KnuticeServer", "Token saved.") + else Log.d("KnuticeServer", "Failed to save token") + } } }