使用 retrofit 获取 arraylist

Getting arraylist by using retrofit

我正在尝试使用 Retrofit 从服务器获取 arayList。 这是我所做的:

适配器类

 public class ItemTypeAdapterFactory implements TypeAdapterFactory {

        public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {

            final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
            final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

            return new TypeAdapter<T>() {

                public void write(JsonWriter out, T value) throws IOException {
                    delegate.write(out, value);
                } 

                public T read(JsonReader in) throws IOException {

                    JsonElement jsonElement = elementAdapter.read(in);
                    if (jsonElement.isJsonObject()) {
                        JsonObject jsonObject = jsonElement.getAsJsonObject();
                        if (jsonObject.has("updates") && jsonObject.get("updates").isJsonArray())
                        {
                            jsonElement = jsonObject.getAsJsonArray("updates");
                        } 
                    } 


                    return delegate.fromJsonTree(jsonElement);
                } 
            }.nullSafe();
        } 
    } 

接口 API

public interface Api {
    @FormUrlEncoded
    @POST("/tools/gps_event_api/")
    public void updateUser(@Field("tag") String tag, @Field("version") String version, Callback<JSONResponse> fbLogin);
}

JsonResponse

public class JSONResponse {
@SerializedName("update")
SaleEven array;
} 

用法:

 Gson gson = new GsonBuilder()
                .registerTypeAdapterFactory(new ItemTypeAdapterFactory()) // This is the important line ;)
                .create();

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://www.poputchik-online.com")
                .setConverter(new GsonConverter(gson))
                .build();

        Api service = restAdapter.create(Api.class);
        retrofit.Callback<JSONResponse> callback=new retrofit.Callback<JSONResponse>() {
            @Override
            public void success(JSONResponse jsonResponse, Response response) {
                Log.e("SUCESS",jsonResponse.toString());
            }

            @Override
            public void failure(RetrofitError error) {
                error.printStackTrace();
            }
        };

        service.updateUser(UPDATE_TAG, "-1",callback);

我的json响应结构:

{
  "tag": "check_update",
  "success": 1,
  "error": 0,
  "updates": [
    {
      "uid": "47",
      "shop_name": "Ashan",
      "shop_address": "\u041c\u043e\u0441\u043a\u043e\u0432\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0441\u043f\u0435\u043a\u0442 25",
      "name": "\u0410\u0448\u0430\u043d",
      "vip_priority": "1",
      "event_type": "0",
      "lat": "52.4978812",
      "lon": "13.4055422",
      "image_url": "uploads\/images3.jpeg",
      "city_id": "2",
      "version": "0",
      "date_from": "2014-12-28 00:00:00",
      "date_to": "2014-12-29 00:00:00",
      "created_at": "2014-07-10 00:00:00",
      "updated_at": "2014-07-10 00:00:00",
      "comment_long": "ewrterwt",
      "comment_short": "wertewrt"
    },
    {
      "uid": "48",
      "shop_name": "Kvadrat",
      "shop_address": "\u0432\u0443\u043b. \u0414\u0438\u043c\u0438\u0442\u0440\u043e\u0432\u0430, 7",
      "name": "\u041a\u0432\u0430\u0434\u0440\u0430\u0442",
      "vip_priority": "1",
      "event_type": "0",
      "lat": "52.7678812",
      "lon": "13.3855422",
      "image_url": "/uploads\/images1.jpeg",
      "city_id": "2",
      "version": "0",
      "date_from": "2014-12-28 00:00:00",
      "date_to": "2014-12-29 00:00:00",
      "created_at": "2014-07-10 00:00:00",
      "updated_at": "2014-07-10 00:00:00",
      "comment_long": "ewrtwret",
      "comment_short": "Privet"
    },

  ]
}

SaleEven 类:

 @SerializedName("uid")
    private long uid;
    @SerializedName("name")
    private String name;
    @SerializedName("comment_short")
    private String comment;
    @SerializedName("comment_long")
    private String longComment;
    @SerializedName("shop_name")
    private String shopName;
    @SerializedName("shop_address")
    private String shopAddress;
....

我收到错误:需要对象但找到数组(Gson)

我想一定是:

public class JSONResponse {
@SerializedName("updates")
List<SaleEven> array;
} 

因为 "updates" 是一个 JSON 数组 -> 将映射到 Java 列表类型。