
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
안녕하세요? 숙제 고양이 서버 연결하기기에서고양이 사진 조회 URL 파라미터 문의 드립니다.
고양이 사진 조회 URL ==> https://api.thecatapi.com/v1/images/search?api_key=live_8QAlZbAf78fW41QwFE77z2BHtkvYwzMHLvjFQACLGp2WaFUQOTXLo4cSwHmP8r51
1. 고양이 사진 조회 URL 호출시 파라미터 api_key밖에 없는데 왜 @Query("limit") num: Int = 1, // num 파라미터를 사용해야 에러가 안 나나요?
확인 부탁 드립니다.
감사합니다.
=============================================================================================================
1. /MainActivity.kt 파일에서
// 고양이 사진 받기 버튼 클릭 이벤트
binding.catButton.setOnClickListener {
Log.d("onCreate", "■ [onCreate()] ==> [T_40] [고양이 사진 받기 버튼 클릭 이벤트]")
lifecycleScope.launch {
val result = RetrofitInstance.service.getImages() // 고양이 사진 호출 ■■■
// val result = RetrofitInstance.service.getImages(num = 4) // 고양이 사진 호출 ■
Log.d("onCreate", "■ [onCreate()] ==> [T_51] [고양이 사진 호출 결과], [data]: $result")
Glide.with(this@MainActivity).load(result[0].url) // result[0].url: 이미지 URL_1
.transition(withCrossFade())
.into(binding.ivAlbum1) // withCrossFade: 좀 더 자연스러운 사용자 UI 제공
Glide.with(this@MainActivity).load(result[1].url) // result[1].url: 이미지 URL_2
.transition(withCrossFade()).into(binding.ivAlbum2)
Glide.with(this@MainActivity).load(result[2].url) // result[2].url: 이미지 URL_3
.transition(withCrossFade()).into(binding.ivAlbum3)
Glide.with(this@MainActivity).load(result[3].url) // result[3].url: 이미지 URL_4
.transition(withCrossFade()).into(binding.ivAlbum4)
}
}
--------------------------------------------------------------------------------------------------------------------------
2. /LottoService.kt 파일에서
package com.example.lovecat.api
import com.example.lovecat.data.CatModel
import retrofit2.http.GET
import retrofit2.http.Query
interface CatService {
@GET("v1/images/search") // GET: HTTP 요청 방식, 엔드 포인트
suspend fun getImages(
// @Query("limit") num: Int = 1, // num 파라미터
// @Query("num") num: Int = 1, // num 파라미터 ■■■
@Query("api_key") api_key: String = "live_8QAlZbAf78fW41QwFE77z2BHtkvYwzMHLvjFQACLGp2WaFUQOTXLo4cSwHmP8r51" // api_key 파라미터
): CatModel // Lotto Model 호출
}
--------------------------------------------------------------------------------------------------------------------------
- Logcat 디버깅 @@
2024-06-27 09:32:24.259 17138-17138 WindowOnBackDispatcher com.example.lovecat W OnBackInvokedCallback is not enabled for the application.
Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
2024-06-27 09:32:24.301 17138-17138 AndroidRuntime com.example.lovecat E FATAL EXCEPTION: main
Process: com.example.lovecat, PID: 17138
java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 ■■■■ ■■■
at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.util.Objects.checkIndex(Objects.java:359)
at java.util.ArrayList.get(ArrayList.java:434)
at com.example.lovecat.MainActivity$onCreate$1$1.invokeSuspend(MainActivity.kt:56)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@d13f494, Dispatchers.Main.immediate]
2024-06-27 09:32:24.349 17138-17207 TrafficStats com.example.lovecat D tagSocket(108) with statsTag=0xffffffff, statsUid=-1
2024-06-27 09:32:29.383 17138-17144 example.lovecat com.example.lovecat I Thread[6,tid=17144,WaitingInMainSignalCatcherLoop,Thread*=0x7ff42bc66b20,peer=0x13580db0,"Signal Catcher"]: reacting to signal 3
2024-06-27 09:32:29.383 17138-17144 example.lovecat com.example.lovecat I
2024-06-27 09:32:29.667 17138-17144 example.lovecat com.example.lovecat I Wrote stack traces to tombstoned
==============================================================================================================
.jpg)
