尽管Http请求返回数据,但仍未调用Retrofit 2 onResponse方法

时间:2016-09-22 14:28:44

标签: android retrofit2

我正在进行异步http请求,调用一个Web服务,该服务以JSON格式返回游戏日期列表。我可以在我的日志中看到正在返回数据,但是onResponseonFailure方法都没有被调用。

以下是我的代码片段,后面是显示http请求结果的日志。请注意,被调用的Web服务仅被要求从gameDate表中返回30列中的1列(Games)。表中有两行,并且从日志中看到的两行都返回gameDate列。我试图将游戏日期列表传递回我的主要活动以供显示。

有没有人知道为什么没有达到onResponseonFailure方法?

public interface GamesAPI {

    @GET("/HCDBWebService/HCDBWebService.php?format=json&operation=gameList")
    Call<List<Game>> listGames();

}




private void requestData() {

    Log.d("MainActivity.LOGTAG", "requestData method");

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    GamesAPI api = retrofit.create(GamesAPI.class);

    Call<List<Game>> call = api.listGames();

    call.enqueue(new Callback<List<Game>>() {
        @Override
        public void onResponse(Call<List<Game>> call, Response<List<Game>> response) {
            if(response.isSuccessful()) {
                gameList = response.body();
            } else {
                Log.e("Error code", String.valueOf(response.code()));
                Log.e("Error body", response.errorBody().toString());
            }
        }

        @Override
        public void onFailure(Call<List<Game>> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

LOG OUTPUT:

 D/OkHttp: Date: Thu, 22 Sep 2016 13:42:54 GMT
 D/OkHttp: Server: Apache/2.2.31 (Unix) mod_wsgi/3.5 Python/2.7.12 PHP/7.0.10 mod_ssl/2.2.31 OpenSSL/1.0.2h DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0
 D/OkHttp: X-Powered-By: PHP/7.0.10
 D/OkHttp: Content-Length: 53
 D/OkHttp: Keep-Alive: timeout=5, max=100
 D/OkHttp: Connection: Keep-Alive
 D/OkHttp: Content-Type: application/json
 D/OkHttp: [{"gameDate":"2016-09-15"},{"gameDate":"2016-09-19"}]
 D/OkHttp: <-- END HTTP (53-byte body)

1 个答案:

答案 0 :(得分:0)

也许为时已晚,但我尝试回答您的问题。您与List <..>有关的问题。只需将所有**List<Game>**更改为**Game**即可使用。应该会有帮助。

示例:

来自

Call<List<Game>> call = api.listGames();

收件人

Call<Game> call = api.listGames();