android - 在 Kotlin Android 中使用 Apollo 客户端使用 GraphQL API 时无法执行 HTTP 调用

标签 android kotlin graphql apollo-client

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

这是我的代码

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."
                }
            })
        }

当我点击按钮时,消息只显示“Started making request”。花了一段时间,他们的响应错误说,无法执行 HTTP 调用。我的代码有什么问题,我该如何解决?

以下是我在manifest 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"/>

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

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

最佳答案

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

引起:java.net.UnknownHostException:无法解析主机

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

关于android - 在 Kotlin Android 中使用 Apollo 客户端使用 GraphQL API 时无法执行 HTTP 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57947407/

相关文章:

reactjs - 如何在 Webpack 构建过程中将 React 应用程序中的 mockServiceWorker.js 文件复制到我的构建文件夹

reactjs - Relay、Redux、Apollo 与 GraphQL 和 React-Native

Android 存储库模式 : how pass extra data to Fragment/Activity

kotlin - 如何在Kotlin中测试使用readline(System .`in`)的代码?

android - 试图在另一个 Activity 中将两个 EditTexts 的产品显示为 TextView

string - 删除 kotlin 中的重音和变音符号

android - 重写onDataChange函数时如何在外部范围内进行更改

android - 使用android时无法解析.xml中的符号@+id/:below_

Android:将数据传递到自定义 View 类中?

python - 如何使用 python 请求使用 graphql 上传文件?