【问题标题】:Implementing Retrofit实施改造
【发布时间】:2017-11-22 22:17:38
【问题描述】:

我已经实现了 Retrofit 从服务返回一些数据,但是大小为 0,在 getGamesData 函数中它不会进入回调方法。

我不确定我声明 BASE_URL 的方式是否正确,以及 ApiInterface 方法 GET 是否正确。

根本不调用 OnResponse 和 OnFailure 方法。

请推荐

返回数据的 URL。

https://dl.dropboxusercontent.com/s/abcd/gameData

它会返回一个像这样的 JSON。

{
    "response": "success",
    "currency" : "GBP",
    "data" : [
        {
            "name": "Game 1",
            "jackpot": 34000000,
            "date": "2015-01-25T20:20:30+01:00"
        },
        {
            "name": "Game 2",
            "jackpot": 100000000,
            "date": "2015-02-16T08:40:30+01:00"
        },
        {
            "name": "Game 3",
            "jackpot": 100000,
            "date": "2015-11-09T10:25:30+01:00"
        },
        {
            "name": "Game 4",
            "jackpot": 45000000,
            "date": "2015-03-10T18:55:30+01:00"
        },
        {
            "name": "Game 5",
            "jackpot": 60000000,
            "date": "2015-07-20T03:45:30+01:00"
        },
        {
            "name": "Game 6",
            "jackpot": 95000000,
            "date": "2015-06-22T09:40:30+01:00"
        },
        {
            "name": "Game 7",
            "jackpot": 100000000,
            "date": "2015-10-19T08:30:30+01:00"
        },
        {
            "name": "Game 8",
            "jackpot": 12000,
            "date": "2015-12-06T07:20:30+01:00"
        }
    ]
}

ApiClient

public class ApiClient {
    public static final String BASE_URL = "https://dl.dropboxusercontent.com/s/abcd/";
    public static Retrofit retrofit = null;
    public  static Retrofit getApiClient()
    {
        if(retrofit == null)
        {
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL).
                    addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }
}

ApiInterface - 更新

   public interface ApiInterface {
    @GET("/gameData")
    Call<GamesWebOrbEntity> getGamesData();
}

ApiCalls - 更新

public class ApiCalls implements IApiCalls{
    private ApiInterface apiInterface;
    private GamesWebOrbEntity gamesEntities;

    @Override
    public GamesWebOrbEntity getGamesData() {
        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<GamesWebOrbEntity> call = apiInterface.getGamesData();
        try{
            call.enqueue(new Callback<GamesWebOrbEntity>() {
                @Override
                public void onResponse(Call<GamesWebOrbEntity> call, Response<GamesWebOrbEntity> response) {
                    gamesEntities = response.body();
                }
                @Override
                public void onFailure(Call<GamesWebOrbEntity> call, Throwable t) {
                    String test = "Failure";

                }
            });
        }catch (Exception e){
            System.out.println("Error " + e.getMessage());
        }
        return gamesEntities;
    }
}

IApiCalls - 更新

public interface IApiCalls {

    GamesWebOrbEntity getGamesData();
}

GamesWebOrbEntity _ 已更新

public class GamesWebOrbEntity {

    @SerializedName("response")
    private String response;
    @SerializedName("currency")
    private String currency;
    @SerializedName("data")
    private List<GameEntity> gameEntities;
}

游戏实体

public class GameEntity {
    private String name;
    private Integer jackpot;
    private Date date;
}

使用它

List<GamesWebOrbEntity> gamesEntity = apiCalls.getGamesData();

谢谢 回复

【问题讨论】:

    标签: android retrofit retrofit2


    【解决方案1】:

    您的 JSON 数据与您的 Java POJO 不匹配。 您收到的不是对象列表,而是一个对象,其中包含一个对象列表,键为 data

    您的 Java 类应该看起来像这样:

    public class GamesWebOrbEntity {
    
        @SerializedName("response")
        private String response;
        @SerializedName("currency")
        private String currency;
        @SerializedName("data")
        private List<Game>;
    }
    public class Game {
     // name
     // jackpot
     // date
    }
    

    【讨论】:

    • 是否必须使用来自 JSON 的所有字段,我的印象是我们可以省略不需要的字段。如果我错了,请纠正我
    • 不,这不是强制性的,您可以省略任何字段。但是您的类的一般结构必须与 JSON 数据的结构相匹配;例如当 JSON 是一个对象时,您需要将其反序列化为一个对象(而不是您正在尝试的对象列表)。你检查你的 LogCat 是否有任何错误?可能也有帮助。
    • 按照建议,我已经进行了更改,但仍然返回 0,我没有任何错误。
    • 请将您的问题更新为您所做的更改。
    • 我已根据建议的更改更新了我的问题。
    【解决方案2】:
    public class GamesWebOrbEntity {
    
        @Expose
        @SerializedName("response")
        private String response;
    
        @Expose
        @SerializedName("currency")
        private String currency;
    
        @Expose
        @SerializedName("data")
        private List<Game>;
    
    
    public String getResponse() {
    return response;
    
    public String getCurrency() {
    return currency;
    }
    
    public List<Game> getData() {
    return data;
    }
    
    }
    
    public class Game {
    
        @Expose
        @SerializedName("name")
        private String name;
    
        @Expose
        @SerializedName("jackpot")
        private String jackpot;
    
        @Expose
        @SerializedName("date")
        private String date;
    
    public String getName() {
    return name;
    }
    
    public int getJackpot() {
    return jackpot;
    }
    
    
    public String getDate() {
    return date;
    }
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      public interface ApiInterface {
          @GET("gameData")
          Call<GamesWebOrbEntity> getGamesData();
      }
      

      去掉gameData前的“/”,服务器给你一个对象,而不是一个列表

      public class GamesWebOrbEntity {
          private String response;
          private String currency;
          private List<GameEntity> gameEntities;
      }
      

      和:

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

      【讨论】:

      • 试过了,但还是不行。顺便提一下,代码不会进入 onResponse 方法,在 call.enqueue(new Callback>() { 之后,它会跳过 try catch。
      • 它不是一个列表,而是一个对象,所以请删除列表
      • @BRDroid 你确实在你的 catch 块中打印了错误,它说了什么?
      • @Ascorbin 没有错误,它没有捕获任何东西,它在 call.enqueue(new Callback>() { 之后跳过
      • @BRDroid 所以onResponse和onFailure都没有被调用?
      猜你喜欢
      • 2011-06-05
      • 2014-03-15
      • 2016-06-27
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2019-12-16
      • 2022-07-16
      • 1970-01-01
      相关资源
      最近更新 更多