質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1980閲覧

Android okhttp3で非同期すると落ちる

s_nakagawa

総合スコア7

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

1グッド

1クリップ

投稿2020/03/12 09:30

前提・実現したいこと

Android studio(Kotlin)でAPIを使ったSqlServer間のデータの受け渡しをするシステムを作っているのですが、
非同期でHTTP GETをするときにアプリごと落ちてしまします。
何がおかしいのか、原因と解決策を教えてください

発生している問題・エラーメッセージ

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cpey, PID: 15149 java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:150) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103) at java.net.InetAddress.getAllByName(InetAddress.java:1152) at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164) at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129) at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71) at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:197) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:107) at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:75) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:148) at com.example.cpey.HttpUtil.httpGet(HttpUtil.kt:17) at com.example.cpey.MainActivity$onParallelGetButtonClick$1$1.invokeSuspend(MainActivity.kt:35) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) at libcore.io.Linux.android_getaddrinfo(Native Method) at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200) at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)  at java.net.InetAddress.getAllByName(InetAddress.java:1152)  at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49)  at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164)  at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129)  at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71)  at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:197)  at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:107)  at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:75)  at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)  at okhttp3.internal.connection.RealCall.execute(RealCall.kt:148)  at com.example.cpey.HttpUtil.httpGet(HttpUtil.kt:17)  at com.example.cpey.MainActivity$onParallelGetButtonClick$1$1.invokeSuspend(MainActivity.kt:35)  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)  Caused by: android.system.ErrnoException: android_getaddrinfo failed: EPERM (Operation not permitted) at libcore.io.Linux.android_getaddrinfo(Native Method)  at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)  at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200)  at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)  at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)  at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)  at java.net.InetAddress.getAllByName(InetAddress.java:1152)  at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49)  at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164)  at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129)  at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71)  at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:197)  at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:107)  at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:75)  at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)  at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)  at okhttp3.internal.connection.RealCall.execute(RealCall.kt:148)  at com.example.cpey.HttpUtil.httpGet(HttpUtil.kt:17)  at com.example.cpey.MainActivity$onParallelGetButtonClick$1$1.invokeSuspend(MainActivity.kt:35)  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)  I/Process: Sending signal. PID: 15149 SIG: 9

該当のソースコード

fun onParallelGetButtonClick() = GlobalScope.launch(Dispatchers.Main) { try{ val http = HttpUtil() async(Dispatchers.Default) { http.httpGet(URL) }.await().let { val result = Json.parse(it).asObject() val textView = findViewById(R.id.text) as TextView textView.setText(result.get("description").asObject().get("text").asString()) } }catch(e:IOException){ e.printStackTrace() } } object HttpClient { val instance = OkHttpClient() } class HttpUtil { fun httpGet(url : String): String? { val request = Request.Builder() .url(url) .build() val response = HttpClient.instance.newCall(request).execute() // 落ちるところ val body = response.body?.string() return body } }
s.k👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

java.lang.SecurityException: Permission denied (missing INTERNET permission

インターネットを使うというパーミッションが設定されて無いのではないでしょうか.

投稿2020/03/12 11:14

jimbe

総合スコア12670

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

s_nakagawa

2020/03/13 01:00

ご回答ありがとうございます。 私はそもそもAndroidの権限について何も知らなかったのですが、調べてみるとAndroidでは常識中の常識的なシステムだということを思い知らされました。 Androidの基礎についてもっと勉強していこうと思います。 こんな初歩的な問題に回答していただき、とても感謝しています。
jimbe

2020/03/13 02:17

ログメッセージが大抵ヒントになりますので, 色々検索してみることをお勧めします.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問