Kotlin(코틀린)/Trouble Shooting
[Kotlin] 에러 해결: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1668) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
2022. 5. 8. 19:01반응형
문제/에러
2022-05-08 18:49:17.767 5797-5797/com.example.myapplication_kotlin2_https D/AndroidRuntime: Shutting down VM 2022-05-08 18:49:17.769 5797-5797/com.example.myapplication_kotlin2_https E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication_kotlin2_https, PID: 5797 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1668) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103) at java.net.InetAddress.getAllByName(InetAddress.java:1152) at com.android.okhttp.Dns$1.lookup(Dns.java:41) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178) at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86) at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176) at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128) at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248) at com.example.myapplication_kotlin2_https.MainActivity.onCreate$lambda-3(MainActivity.kt:41) at com.example.myapplication_kotlin2_https.MainActivity.$r8$lambda$gTDMxezOd-x6BAT1hD8fuyv1HOo(Unknown Source:0) at com.example.myapplication_kotlin2_https.MainActivity$$ExternalSyntheticLambda1.onClick(Unknown Source:2) at android.view.View.performClick(View.java:7792) at android.widget.TextView.performClick(TextView.java:16112) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194) at android.view.View.performClickInternal(View.java:7769) at android.view.View.access$3800(View.java:910) at android.view.View$PerformClick.run(View.java:30218) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 2022-05-08 18:49:17.806 5797-5797/com.example.myapplication_kotlin2_https I/Process: Sending signal. PID: 5797 SIG: 9 |
원인
안드로이드에서 Policy에 의해, block되어 에러 발생.
해결방법
아래의 소스코드를 onCreate쪽 메서드에 추가한다.
if (Build.VERSION.SDK_INT > 9) {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
}
예를 들어, 아래와 같다.
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
if (Build.VERSION.SDK_INT > 9) {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
}
...
반응형
'Kotlin(코틀린) > Trouble Shooting' 카테고리의 다른 글
(작성중) [코틀린(Kotlin)] 에러해결: android.os.NetworkOnMainThreadException (0) | 2022.09.14 |
---|---|
[코틀린(Kotlin)] 에러해결: java.io.IOException: Cleartext HTTP traffic to openapi.work.go.kr not permitted (0) | 2022.09.13 |