【问题标题】:Getting arraylist by using retrofit使用改造获取arraylist
【发布时间】:2015-01-06 19:04:27
【问题描述】:

我正在尝试使用 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();
        } 
    } 

接口接口

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)

【问题讨论】:

    标签: android gson retrofit


    【解决方案1】:

    我想一定是:

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

    因为“updates”是一个 JSON 数组 -> 将被映射到 Java List 类型。

    【讨论】:

    • 感谢它的帮助。而且我还需要避免为 GSON 使用自定义适配器,即使没有自定义适配器,它也可以工作。
    • 您好,我有类似的 JSON 结构作为响应,我正在努力形成 POJO,我也在使用 GSON。您还提到您必须使用自定义适配器,您是怎么做到的?如果您能解释一下,那就太好了……谢谢
    • @Deep 只使用一些来自 json 的代码生成器,例如jsonschema2pojo.org
    • 如果jsonarray名称不存在怎么办??/
    猜你喜欢
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2014-06-11
    • 2018-04-20
    • 2019-10-28
    相关资源
    最近更新 更多