[ANDROID] Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

CHA·2023년 4월 25일

문제

JSON 파싱 시에 GSON 을 사용하면 나타날 수 있는 오류
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

해결

Retrofit 객체를 만들 때, Gson 객체를 만들어서 추가해주면 해결.

class RetrofitHelper {

    companion object{

        fun getRetrofitInstance(baseUrl: String) : Retrofit{
            val gson: Gson = GsonBuilder()
                .setLenient()
                .create()

            val retrofit = Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()

            return retrofit
        }
    }
}
profile
Developer

0개의 댓글