实施改造

时间:2017-11-22 13:25:32

标签: android retrofit retrofit2

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

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

根本不调用OnResponse和OnFailure方法。

请建议

返回数据的网址。

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 _UpdaTED

public class GamesWebOrbEntity {

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

GameEntity

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

使用它

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

由于 [R

3 个答案:

答案 0 :(得分:0)

您的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
}

答案 1 :(得分:0)

试试这个:

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();

答案 2 :(得分:0)

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;
}
}