관리 메뉴

나만을 위한 블로그

[Android] Kotlin으로 레트로핏 사용 시 나오는 에러 해결법 정리 본문

Android

[Android] Kotlin으로 레트로핏 사용 시 나오는 에러 해결법 정리

참깨빵위에참깨빵 2021. 2. 8. 01:32
728x90
반응형

1. java.lang.NoSuchMethodError: No static method metafactory

 

이 에러는 자바 호환성 중 자바 8버전 호환성이 없기 때문에 발생한다.

앱 수준 gradle에 아래를 추가하면 해결된다.

 

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

 

2. CLEARTEXT communication to xx.xxx.xxx.xx not permitted by network security policy

 

이 에러는 매니페스트의 <application> 태그 안에 android:usesCleartextTraffic="true" 속성 넣어주면 해결된다.

 

 

3. socket failed: EACCES (Permission denied)

 

Permission denied에서 볼 수 있듯 이 에러는 매니페스트에 인터넷 권한을 부여하지 않으면 생기는 에러다.

 

<uses-permission android:name="android.permission.INTERNET" />

위 퍼미션 코드를 복붙하면 해결된다.

 

 

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

 

이 에러는 GsonConverterFactory를 설정해주지 않아서 발생하는 에러다.

 

implementation "com.squareup.retrofit2:converter-gson:2.8.1"

위의 의존성 문구를 앱 수준 gradle에 복붙한 뒤 임포트해주면 해결된다.

그러나 이 에러를 해결한 후 레트로핏으로 데이터 전송 시 아래와 같은 에러가 발생할 수 있다.

 

 

5. JSON document was not fully consumed.

 

이 에러에 대해서 square의 retrofit 깃허브 이슈에서 레트로핏 제작자가 아래와 같이 말했다.

 

이 예외는 Gson이 일반적으로 사용자 정의 유형 어댑터의 버그를 나타내는 응답 본문을 완전히 사용하지 않을 때 발생합니다. 예제 응답 본문, JSON 모델 개체 및 사용자 지정 유형 어댑터가 없으면 문제가 어디에 있는지 알 수 없습니다.

 

근데 무슨 말인지 모르겠다. 대략 모델 클래스에 아래와 같이 써넣으면 해결된다고 한다.

 

@SerializedName("isSend")
private boolean isSend;

@SerializedName("code")
private int code;

하지만 내 경우에는 이런 모델 클래스가 필요없는 곳에서 String으로 값을 가져오려다가 발생했기 때문에 다른 해결법을 찾았다.

해결법은 레트로핏 객체를 생성할 때 다른 컨버터 팩토리를 만드는 것이었다.

 

if (retrofit == null)
        {
            retrofit = Retrofit.Builder()
                .baseUrl(baseURL)
//                .addConverterFactory(GsonConverterFactory.create(gson))
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()
        }

빌드할 때 GsonConverterFactory를 추가하면 ScalarsConverterFactory를 추가하더라도 GsonConverterFactory가 먼저 작동한다. 그러면 필연적으로 에러가 날 수밖에 없다.

그래서 GsonConverterFactory를 주석 처리한 후 ScalarsConverterFactory만을 사용해봤다. 이렇게 하니 에러 없이 정상적으로 작동했다.

 

 

참고한 사이트)

 

stackoverflow.com/questions/54256940/com-google-gson-jsonioexception-json-document-was-not-fully-consumed-in-android

 

com.google.gson.JsonIOException: JSON document was not fully consumed in Android Retrofit

There are couple of questions already available about "JSON document was not fully consumed" in Stackoverflow, but there is no accepted answer. Link is given below. Android Retrofit 2.0 JSON docum...

stackoverflow.com

findfun.tistory.com/654?category=396678

 

안드로이드 ksoap2 웹서비스 사용 시 socket failed: EACCES (Permission denied) 뜰때

안드로이드 첫 개발이다. 왜 시작했지... 걍 웹으로 할 걸~ 모르고 시작했으니 좋은 품질이 나올까 모르겠다. 시나리오 : 1. 서버 : 닷넷 웹서비스 2. 클라이언트 : 안드로이드 앱 3. 로그인 프로토

findfun.tistory.com

github.com/square/retrofit/issues/3004

 

json document was not fully consumed · Issue #3004 · square/retrofit

Hello, I am using retrofit2. Here is my retrofit client private static Gson gson = new GsonBuilder() .setLenient() .create(); public static Retrofit getClient() { if (retrofit == null) { retrofit =...

github.com

ppost.tistory.com/entry/Retrofit2%EB%A1%9C-%EB%8F%84%EB%A1%9C%EB%AA%85%EC%A3%BC%EC%86%8C-%EA%B3%B5%EA%B3%B5%EB%8D%B0%EC%9D%B4%ED%84%B0-API-%EA%B0%80%EC%A0%B8%EC%98%AC%EB%95%8C-%EC%A3%BC%EC%9D%98%ED%95%A0%EC%A0%90?category=152556

 

Retrofit2로 도로명주소 공공데이터 API 가져올때 주의할점

이번에 도로명주소 검색을 구현하면서 이것저것 써봣는데, 다음지도는 웹뷰를 띄워야하고 뷰 커스텀 하기에도 적절치 않아서 찾아보다가 도로명주소 공공데이터 api 를 발견했다. 아무튼 이 api

ppost.tistory.com

simplifyprocess.tistory.com/4

 

[Android] No static method metafactory

Kotlin + Retrofit + OkHttp 예제를 만들고 있는데.. 갑자기 다음과 같은 에러가 나와서 살짝 당황했습니다. java.lang.NoSuchMethodError: No static method metafactory (declaration of 'java.lang.invoke.Lamb..

simplifyprocess.tistory.com

 

반응형
Comments