• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    미해결

Retrofit 관련 Logcat 화면에 아래 예외가 출력

23.10.27 10:02 작성 23.10.27 10:06 수정 조회수 408

0

build.gradle 에 아래를 추가한 후 sync now 하고나서

 

implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

 

아래는 전체코드입니다

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
           val coroutineScope : CoroutineScope = rememberCoroutineScope()
           val retrofitInstance = RetrofitInstance.getInstance().create(MyAPI::class.java)

           Box(
               modifier = Modifier.fillMaxSize(),
               contentAlignment = Alignment.Center,
           ) {
               Button(
                   onClick = {
                       coroutineScope.launch {
                           val response : Response<Post> = retrofitInstance.getPost1()
                           Log.d("MainActivity", response.body().toString())
                       }
                   }
               ) {
                   Text(
                       "Call API"
                   )
               }
           }
        }
    }
}

data class Post(
    val userId : Int,
    val id : Int,
    val title : String,
    val body : String,
)

object RetrofitInstance {
    val BASE_URL = "https://jsonplaceholder.typicode.com/posts/1/"
    val client = Retrofit
        .Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    fun getInstance() : Retrofit {
        return client
    }
}

interface MyAPI {
    @GET("posts/1")
    suspend fun getPost1() : Response<Post>
}

 

 위를 실행하니

Logcat 화면에 아래 예외가 출력되고 데이터를 얻어오지 못하는 군요

RemoteConnection failed to initialize: RemoteConnection failed to open pipe

Failed to open QEMU pipe 'qemud:network': Invalid argument

sendWakeUpEvent called on SchedulerClientBroadcastStrategy

GCM_HB_ALARM release without a matched acquire!

 Couldn't load memtrack module

 

수고많으신 강사님

늘 건강하시고 부자되세요

 

답변 6

·

답변을 작성해보세요.

0

shafeel2님의 프로필

shafeel2

질문자

2023.10.30

알고 보니 코드상에는 문제가 없구요

제가 실수로 안드로이드 스튜디오의

Logcat Pause 버튼을 잘못눌러서

결과물 출력이 않되었군요

 

강사님과 여기 수강하시는 모든 분들

늘 건강하시고 부자되세요 ^^

0

shafeel2님의 프로필

shafeel2

질문자

2023.10.28

q8.png

Logcat 에는 아무런 출력이 없고

Problems 화면에 위와 같은 출력이 발생합니다

로그캣에 안나오는 문제는 안드로이드 스튜디오 재시작 / 에뮬레이터 새롭게 만들어서 시도해보시겠어요?

0

shafeel2님의 프로필

shafeel2

질문자

2023.10.28

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
           val coroutineScope : CoroutineScope = rememberCoroutineScope()
           val retrofitInstance = RetrofitInstance.getInstance().create(MyAPI::class.java)

           Box(
               modifier = Modifier.fillMaxSize(),
               contentAlignment = Alignment.Center,
           ) {
               Button(
                   onClick = {
                       coroutineScope.launch {
                           val response : Response<Post> = retrofitInstance.getPost1()
                           Log.d("MainActivity", response.body().toString())
                       }
                   }
               ) {
                   Text(
                       "Call API"
                   )
               }
           }
        }
    }
}

data class Post(
    val userId : Int,
    val id : Int,
    val title : String,
    val body : String,
)

object RetrofitInstance {
    val BASE_URL = "https://jsonplaceholder.typicode.com/"
    val client = Retrofit
        .Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    fun getInstance() : Retrofit {
        return client
    }
}

interface MyAPI {
    @GET("posts/1/")
    suspend fun getPost1() : Response<Post>
}

위와 같이 수정하면

Property 'BASE_URL' could be private

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.

Property 'client' could be private

라는 메세지가 problems 창에 출력되고 여전히 Logcat 창에는 아무런 출력이 없군요

안드로이드 스튜디오를 재시작해도 그런가요?

저는 동일한 코드로 잘 나오는것을 확인했습니다.

image

안드로이드 스튜디오 재시작 / 에뮬레이터 재실행 후에도 로그가 안나오면 로그창을 스크린샷으로 공유해주세요.

0

BASE_URL을 아래와 같이 변경해보세요~

val BASE_URL = "https://jsonplaceholder.typicode.com/"

 

 

 

0

shafeel2님의 프로필

shafeel2

질문자

2023.10.27

0

전체 프로젝트를 압축하셔서 구글 드라이브에 올리신 후 링크를 공유해주시면 살펴볼게요~