-
카테고리
-
세부 분야
모바일 앱 개발
-
해결 여부
미해결
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
수고많으신 강사님
늘 건강하시고 부자되세요
답변을 작성해보세요.
0
shafeel2
질문자2023.10.30
알고 보니 코드상에는 문제가 없구요
제가 실수로 안드로이드 스튜디오의
Logcat Pause 버튼을 잘못눌러서
결과물 출력이 않되었군요
강사님과 여기 수강하시는 모든 분들
늘 건강하시고 부자되세요 ^^
0
0
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 창에는 아무런 출력이 없군요
0
개복치개발자
지식공유자2023.10.28
BASE_URL을 아래와 같이 변경해보세요~
val BASE_URL = "https://jsonplaceholder.typicode.com/"
0
shafeel2
질문자2023.10.27
0
답변 6