본문 바로가기
🖥 Programming/📱 Android (Kotlin)

[Android][kotlin] Retrofit End of input at line 1 column 1 path $ 해결방법

by MinChan-Youn 2022. 8. 25.

안녕하세요~ 챠니입니다! :)

이번에는 Retrofit 통신을 구현하면서 발상해는 원인에 대해서 알아보겠습니다.

 

 

End of input at line 1 column 1 path $  오류?

End of input at line 1 column 1 path $
Retrofit에서 Response값이 비어있을 때(Length=0) 발생하는 오류입니다.

해결방법은 다음과 같습니다.

 

 

 

End of input at line 1 column 1 path $  해결방법

End of input at line 1 column 1 path $
다음의 코드를 추가합니다.
Retrofit Builder하는 부분에서 다음 표시된 부분을 추가합니다.
val retrofit = Retrofit.Builder()
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .addConverterFactory(nullOnEmptyConverterFactory)
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(BASE_PATH)
    .build()
interface RetrofitService{
    companion object {
        var networkService: RetrofitService? = null
        var baseUrl = "http://*********"

        fun getInstance(): RetrofitService {
            if (networkService == null) {
                val retrofit = Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(nullOnEmptyConverterFactory)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                networkService = retrofit.create(RetrofitService::class.java)
            }
            return networkService!!
        }

        /** 비어있는(length=0)인 Response를 받았을 경우 처리 */
        private val nullOnEmptyConverterFactory = object : Converter.Factory() {
            fun converterFactory() = this
            override fun responseBodyConverter(type: Type, annotations: Array<out Annotation>, retrofit: Retrofit) = object : Converter<ResponseBody, Any?> {
                val nextResponseBodyConverter = retrofit.nextResponseBodyConverter<Any?>(converterFactory(), type, annotations)
                override fun convert(value: ResponseBody) = if (value.contentLength() != 0L) {
                    try{
                        nextResponseBodyConverter.convert(value)
                    }catch (e:Exception){
                        e.printStackTrace()
                        null
                    }
                } else{
                    null
                }
            }
        }
    }
}

 

 

 

 

 

 

 

질문 또는 궁굼한 부분은 댓글을 남겨주세요! 친절하게 답변드리겠습니다!

응원의 댓글은 저에게 큰 힘이 된답니다! :)

즐거운 하루되세요!

 

깃허브 보러 놀러오세요 👇 (맞팔환영)

https://github.com/younminchan

 

younminchan - Overview

안드로이드 2년차 개발자 •⚽️/🎤/🥁/🖥/🏃‍♂️/🚴‍♂️/🤟 TechBlog⬇️ minchanyoun.tistory.com - younminchan

github.com