使用改造版Android创建特定Entity类的Json响应

时间:2019-03-23 17:46:28

标签: android json retrofit

我有一个Entity类,我想创建一个应该由该实体类解析的json响应,但是我无法创建确切的json响应,并且在解析json数据时得到null。我无法更改实体,所以我想创建一个完全匹配的json响应。

实体类

 import com.googlecode.objectify.annotation.Entity;
 import com.googlecode.objectify.annotation.Id;
 import com.googlecode.objectify.annotation.Index;

 import java.util.Calendar;
 import java.util.UUID;

 @Entity
 public class Product {

public enum Type {
    ALL, LEAFY, FRUITY
}

public enum Volume {
    WEIGHT("gms"), QUANTITY("pcs");
    private final String extension;

    Volume(String extension) {
        this.extension = extension;
    }

    public String getExtension() {
        return extension;
    }
}

@Id
private String id;
@Index
private String name_english;
private String name_hinglish;
private int minimumVolume;
private int maximumVolume;
private int volumeSet; // Fruits can be purchased in 3, 6, 9, 12 quantity
private String imageUrl;
private int price;
@Index
private long time;
@Index
private Type type;
@Index
private Volume volume;

public Product() {
}

public Product(String name_english, String name_hinglish, Type type, Volume volume, int price) {
    this.id = UUID.randomUUID().toString();
    this.name_english = name_english;
    this.name_hinglish = name_hinglish;
    this.type = type;
    this.volume = volume;
    this.price = price;
    this.maximumVolume = 0;
    this.minimumVolume = 0;
    this.volumeSet = 0;
    this.imageUrl = "";
    this.time = Calendar.getInstance().getTimeInMillis();
}

public int getPrice() {
    return price;
}

public long getTime() {
    return time;
}

public void setPrice(int price) {
    this.price = price;
}

public void setTime(long time) {
    this.time = time;
}

public void setId(String id) {
    this.id = id;
}

public void setName_english(String name_english) {
    this.name_english = name_english;
}

public void setName_hinglish(String name_hinglish) {
    this.name_hinglish = name_hinglish;
}

public void setType(Type type) {
    this.type = type;
}

public void setVolume(Volume volume) {
    this.volume = volume;
}

public void setMinimumVolume(int minimumVolume) {
    this.minimumVolume = minimumVolume;
}

public void setMaximumVolume(int maximumVolume) {
    this.maximumVolume = maximumVolume;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public String getImageUrl() {
    return imageUrl;
}

public int getMinimumVolume() {
    return minimumVolume;
}

public int getMaximumVolume() {
    return maximumVolume;
}

public void setVolumeSet(int volumeSet) {
    this.volumeSet = volumeSet;
}

public int getVolumeSet() {
    return volumeSet;
}

public String getId() {
    return id;
}

public String getName_english() {
    return (String.valueOf(name_english.charAt(0)).toUpperCase()
            .concat(name_english.substring(1).toLowerCase()));
}

public String getName_hinglish() {
    return name_hinglish;
}

public Type getType() {
    return type;
}

public Volume getVolume() {
    return volume;
}

public boolean isEmpty() {
    return (getId() == null || getId().isEmpty());
}

}

当前json响应

[{"id":1,"name_english":"Tomato","name_hinglish":"Tamatar","minimumVolume":10,"maximumVolume":20,"volumeSet":3,"imageUrl":"Tamatar","price":25,"time":"10:55","type":"ALL","volume":"gms"},{"id":1,"name_english":"Potato","name_hinglish":"Tamatar","minimumVolume":12,"maximumVolume":60,"volumeSet":12,"imageUrl":"Tamatar","price":25,"time":"10:55","type":"ALL","volume":"pcs"},{"id":1,"name_english":"Carrot","name_hinglish":"Tamatar","minimumVolume":10,"maximumVolume":20,"volumeSet":3,"imageUrl":"Tamatar","price":25,"time":"10:55","type":"ALL","volume":"pcs"}]

改装接口代码

 @GET("bhajipala.php")
 Call<EntityList<Product>> getProductInfoList();

改装代码:

   private static final String BASE_URL = "http://139.59.77.124/Bhajipala/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

响应解析

      Response<EntityList<Product>> response = call.execute();
        if (response.body() != null && response.body().getItems() != null) {
            // we get Items
            if (response.body().getItems().size() > 0) {
                // store to database
                dbHandler.storeProducts(response.body().getItems());
                broadcast(ACTION_SUCCESS);
            } else { // server sends empty list
                Log.e(TAG, "Empty product list");
                broadcast(ACTION_EMPTY);
            }
        } else {// bad response
            Log.e(TAG, "Bad response " + response.errorBody());
            broadcast(ACTION_ERROR);
        }

0 个答案:

没有答案