Apollo GraphQL Android 无法解决“无法解析 http 响应”错误

Posted

技术标签:

【中文标题】Apollo GraphQL Android 无法解决“无法解析 http 响应”错误【英文标题】:Apollo GraphQL Android Cannot resolve "Failed to parse http response" error 【发布时间】:2019-03-16 03:31:14 【问题描述】:

我面临的问题是它引发了Failed to parse http response 错误。我不确定这里有什么问题?我遵循了文档,但它似乎已经过时了。

代码

public class AniList 
    private static final String BASE_URL = "https://graphql.anilist.co";

    public void run() 
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

        ApolloClient apolloClient = ApolloClient.builder()
            .serverUrl(BASE_URL)
            .okHttpClient(okHttpClient)
            .build();

        MediaQuery mediaQuery = MediaQuery.builder()
            .username("ojiii")
            .type(MediaType.ANIME)
            .build();

        apolloClient.query(mediaQuery).enqueue(new ApolloCall.Callback <MediaQuery.Data> () 
            @Override public void onResponse(@NotNull Response <MediaQuery.Data> dataResponse) 
                Log.d("Data", dataResponse.toString());
            

            @Override
            public void onFailure(@NotNull ApolloException t) 
                Log.e(TAG, t.getMessage(), t);
            
        );

    

GraphQL

query MediaQuery($username: String, $type: MediaType) 
    MediaListCollection(userName: $username, type: $type) 
      statusLists 
        progress
        progressVolumes
        score
        status
        notes
        repeat
        media 
          chapters
          volumes
          idMal
          episodes
          title  romaji 
        
      
    

```

错误

2018-10-10 12:55:26.058 14893-14911/sukebei.anilog E/Constraints: Failed to parse http response
    com.apollographql.apollo.exception.ApolloParseException: Failed to parse http response
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.parse(ApolloParseInterceptor.java:101)
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onResponse(ApolloParseInterceptor.java:53)
        at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$1$1.onResponse(ApolloServerInterceptor.java:94)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
     Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
        at com.apollographql.apollo.internal.response.RealResponseReader.readList(RealResponseReader.java:153)
        at AniList.MediaQuery$MediaListCollection$Mapper.map(MediaQuery.java:379)
        at AniList.MediaQuery$Data$Mapper$1.read(MediaQuery.java:278)
        at AniList.MediaQuery$Data$Mapper$1.read(MediaQuery.java:275)
        at com.apollographql.apollo.internal.response.RealResponseReader.readObject(RealResponseReader.java:138)
        at AniList.MediaQuery$Data$Mapper.map(MediaQuery.java:275)
        at AniList.MediaQuery$Data$Mapper.map(MediaQuery.java:270)
        at com.apollographql.apollo.response.OperationResponseParser$1.read(OperationResponseParser.java:96)
        at com.apollographql.apollo.internal.json.ResponseJsonStreamReader.nextObject(ResponseJsonStreamReader.java:86)
        at com.apollographql.apollo.response.OperationResponseParser.parse(OperationResponseParser.java:91)
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.parse(ApolloParseInterceptor.java:87)
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onResponse(ApolloParseInterceptor.java:53) 
        at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$1$1.onResponse(ApolloServerInterceptor.java:94) 
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) 
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:761) 

【问题讨论】:

【参考方案1】:

问题是我正在请求一个已弃用的、graphql 不兼容的数据结构,它仍然返回数据。

如果您遇到此问题,请确保您的 graphql 查询与 grapql 兼容。

【讨论】:

以上是关于Apollo GraphQL Android 无法解决“无法解析 http 响应”错误的主要内容,如果未能解决你的问题,请参考以下文章

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

Apollo 客户端无法连接到 GraphQL 服务器(http 调用执行失败)

GraphQL / Apollo - 无法获取 NetworkError

在 apollo (graphql) 查询中无法识别 FlowRouter

身份验证Apollo Graphql for android

使用带有 Kotlin 和流的 Android 上的 Apollo 订阅 GraphQL 时处理网络错误