在 Kotlin Android 中使用 Apollo Client 消费 GraphQL API 时无法执行 HTTP 调用

Wai*_*ein 3 android kotlin graphql apollo-client

我正在使用 Kotlin 开发 Android 应用程序。我正在尝试在我的应用程序中使用 Apollo Client 使用 GraphQL API。我安装了 Apollo 客户端并成功生成了架构和类。现在我正在发出突变请求,但它没有返回任何响应。

这是我的代码

view.btn_login.setOnClickListener {
            val okHttp = OkHttpClient
                .Builder()
                .addInterceptor({ chain ->
                    val original = chain.request()
                    val builder = original.newBuilder().method(original.method(),
                        original.body())
                    chain.proceed(builder.build())
                })
                .build()
            val apolloClient = ApolloClient.builder()
                .serverUrl("https://my-app-name.herokuapp.com/graphql")
                .okHttpClient(okHttp)
                .build()

            val loginMutation = LoginMutation.builder()
                .identity(view.etf_email.text.toString())
                .password(view.etf_password.text.toString())
                .build()

            view.tv_login_error_message.text = "Started making request"
            apolloClient.mutate(loginMutation).enqueue(object: ApolloCall.Callback<LoginMutation.Data>() {
                override fun onFailure(e: ApolloException) {
                    view.tv_login_error_message.text = e.message
                }

                override fun onResponse(response: Response<LoginMutation.Data>) {
                    view.tv_login_error_message.text = "Request completed."
                }
            })
        }
Run Code Online (Sandbox Code Playgroud)

当我单击按钮时,消息仅显示“开始发出请求”。花了一段时间,响应错误说,无法执行 HTTP 调用。我的代码有什么问题以及如何修复它?

以下是我在清单xml中添加的权限。

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.CAMERA"/>
Run Code Online (Sandbox Code Playgroud)

这是我在堆栈跟踪中得到的

2019-09-15 22:54:13.027 20508-20580/com.example.memento E/MYAPP: exception
2019-09-15 22:54:13.027 20508-20580/com.example.memento W/System.err: com.apollographql.apollo.exception.ApolloNetworkException: Failed to execute http call
2019-09-15 22:54:13.028 20508-20580/com.example.memento W/System.err:     at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$2.onFailure(ApolloServerInterceptor.java:120)
2019-09-15 22:54:13.028 20508-20580/com.example.memento W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:211)
2019-09-15 22:54:13.028 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2019-09-15 22:54:13.028 20508-20580/com.example.memento W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at java.lang.Thread.run(Thread.java:919)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err: Caused by: java.net.UnknownHostException: Unable to resolve host "memento-nova-api-staging.herokuapp.com": No address associated with hostname
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:1152)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at okhttp3.Dns$1.lookup(Dns.java:40)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
2019-09-15 22:54:13.029 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214)
2019-09-15 22:54:13.030 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
2019-09-15 22:54:13.031 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
2019-09-15 22:54:13.031 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
2019-09-15 22:54:13.031 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-09-15 22:54:13.031 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-09-15 22:54:13.031 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
2019-09-15 22:54:13.032 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-09-15 22:54:13.032 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-09-15 22:54:13.032 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2019-09-15 22:54:13.032 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-09-15 22:54:13.032 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
2019-09-15 22:54:13.033 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-09-15 22:54:13.033 20508-20580/com.example.memento W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-09-15 22:54:13.033 20508-20580/com.example.memento W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:250)
2019-09-15 22:54:13.033 20508-20580/com.example.memento W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:201)
2019-09-15 22:54:13.033 20508-20580/com.example.memento W/System.err:   ... 4 more
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:     at libcore.io.Linux.android_getaddrinfo(Native Method)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:     at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)
2019-09-15 22:54:13.036 20508-20580/com.example.memento W/System.err:   ... 26 more
Run Code Online (Sandbox Code Playgroud)

gme*_*tal 5

查看您发布的堆栈跟踪后,我相信错误的实际原因是以下行:

Caused by: java.net.UnknownHostException: Unable to resolve host

您的设备似乎无法通过 DNS 解析服务器 URL。这意味着您的 DNS 服务器可能无法访问,或者您输入了错误的服务器 URL,或者您的设备由于某种原因(例如防火墙)无法访问 DNS 服务器。我会在设备内打开浏览器并尝试连接到服务器(这样我就可以建立连接)。