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

时间:2018-10-11 06:18:36

标签: java android apollo apollo-client

我面临的问题是它引发了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 个答案:

答案 0 :(得分:0)

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

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