0.1. ⚠️ 경고 ⚠️
1. 😆 결과 (짜자잔~)
2. 🥵 부록 : Value --> of type java.lang.String cannot be converted to JSONObject
728x90
반응형
0.1. ⚠️ 경고 ⚠️
본 카테고리, Dev Memo는 필자가 깊게 다루기는 귀찮지만 궁금한 것들을 체험해보고 간단하게 기록을 남기는 공간입니다.
디버깅 노트, 써드파티 라이브러리 사용기, 버전 업데이트, 어이없는 실수, 오탈자 발견 등
각종 시덥지 않은 내용이 들어 갈 수 있다는 점 참고바랍니다. (우헤헿)


우연히 "Logger로 안드로이드 로그향상하기"라는 글을 보았다. 개발자라면 무수한 로그를 보는데 조금이라도 깨끗한 + 정리된 + 예쁜 로그를 볼수록 정신건강에 좋다. 그래서 따라해보았다.
// build.gradle (app) // OkHttp implementation "com.squareup.okhttp3:okhttp:$okhttpVersion" implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion" // Logger implementation "com.orhanobut:logger:$loggerVersion"
kotlin
// Application 레벨에서 Logger.addLogAdapter(object : AndroidLogAdapter() { override fun isLoggable(priority: Int, tag: String?): Boolean { return BuildConfig.DEBUG } }) // 테스트용 Logger.d("hello")
kotlin
// 실제 적용 사례 val client = OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor( HttpLoggingInterceptor.Logger { try { JSONObject(it) Logger.t("INTERCEPTOR").json(it) } catch (error: JSONException) { Logger.t("INTERCEPTOR").i(it) } } ).setLevel(HttpLoggingInterceptor.Level.BODY)).build() Retrofit.Builder() .baseUrl("https://dapi.kakao.com") .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build() .create(KakaoSearchService::class.java)
kotlin

1. 😆 결과 (짜자잔~)
TAG가 들어가는 Verbose 부분을 잘 보라. "PRETTY_LOGGER"라고 입력하면 예쁜 로그 출력을 볼 수 있다.

2. 🥵 부록 : Value --> of type java.lang.String cannot be converted to JSONObject
만약 위와 같은 에러 메시지가 나타날 경우, JSONException 처리가 원인이다.
// before JSONObject(it) Logger.t("INTERCEPTOR").json(it) // after try { JSONObject(it) Logger.t("INTERCEPTOR").json(it) } catch (error: JSONException) { Logger.t("INTERCEPTOR").i(it) }
kotlin
참고자료
- Logger로 안드로이드 로그향상하기
- Retrofit2 와 okHttp를 사용하여 통신을해보자
- Retrofit 으로 네이버 API 사용해보기
- OkHttp Homepage
- Logger github (가장 중요 ♥️)
728x90
반응형
'Dev Memo' 카테고리의 다른 글
스프링에서 local.yml 또는 dev.yml 환경으로 전환하고 싶을 때 (0) | 2021.11.29 |
---|---|
ant-design의 typescript 버전이 보이지 않는 경우 (0) | 2021.11.03 |
AWS 구성 설정(aws configure)을 하지 않았을 때 나타나는 상황 (1) | 2021.08.29 |
[Spring] HTTP Request 전송 시, Broken pipe 이슈 (0) | 2021.08.15 |
License for package Android SDK Build-Tools not accepted. (0) | 2021.03.18 |
Comment