Gson comment sérialiser une classe qui implémente une interface ?
Bonjour à tous,
J'ai besoin d'utiliser une interface dans une classe POJO, lorsque j'utilise Gson sur Android j'ai cette erreur:
12-06 06:18:40.875: E//DefaultRequestRunner.java:154(2607): Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.example.productrobospice.model.Item. Register an InstanceCreator with Gson for this type may fix this problem.
Voici ma classe Pojo:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| package com.example.productrobospice.model;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.productrobospice_db.R;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Product implements Item {
public Product() {}
@SerializedName("libelle_produit")
@Expose
private String libelleProduit;
@SerializedName("prix_ttc")
@Expose
private String prixTtc;
@SerializedName("description_short")
@Expose
private String descriptionShort;
@Expose
private String condition;
@SerializedName("online_only")
@Expose
private String onlineOnly;
@SerializedName("on_sale")
@Expose
private String onSale;
@Expose
private String reduction;
@SerializedName("reduction_type")
@Expose
private String reductionType;
@SerializedName("meta_data")
@Expose
private MetaData metaData;
/**
*
* @return The libelleProduit
*/
public String getLibelleProduit() {
return libelleProduit;
}
/**
*
* @param libelleProduit
* The libelle_produit
*/
public void setLibelleProduit(String libelleProduit) {
this.libelleProduit = libelleProduit;
}
/**
*
* @return The prixTtc
*/
public String getPrixTtc() {
return prixTtc;
}
/**
*
* @param prixTtc
* The prix_ttc
*/
public void setPrixTtc(String prixTtc) {
this.prixTtc = prixTtc;
}
/**
*
* @return The descriptionShort
*/
public String getDescriptionShort() {
return descriptionShort;
}
/**
*
* @param descriptionShort
* The description_short
*/
public void setDescriptionShort(String descriptionShort) {
this.descriptionShort = descriptionShort;
}
/**
*
* @return The condition
*/
public String getCondition() {
return condition;
}
/**
*
* @param condition
* The condition
*/
public void setCondition(String condition) {
this.condition = condition;
}
/**
*
* @return The onlineOnly
*/
public String getOnlineOnly() {
return onlineOnly;
}
/**
*
* @param onlineOnly
* The online_only
*/
public void setOnlineOnly(String onlineOnly) {
this.onlineOnly = onlineOnly;
}
/**
*
* @return The onSale
*/
public String getOnSale() {
return onSale;
}
/**
*
* @param onSale
* The on_sale
*/
public void setOnSale(String onSale) {
this.onSale = onSale;
}
/**
*
* @return The reduction
*/
public String getReduction() {
return reduction;
}
/**
*
* @param reduction
* The reduction
*/
public void setReduction(String reduction) {
this.reduction = reduction;
}
/**
*
* @return The reductionType
*/
public String getReductionType() {
return reductionType;
}
/**
*
* @param reductionType
* The reduction_type
*/
public void setReductionType(String reductionType) {
this.reductionType = reductionType;
}
/**
*
* @return The metaData
*/
public MetaData getMetaData() {
return metaData;
}
/**
*
* @param metaData
* The meta_data
*/
public void setMetaData(MetaData metaData) {
this.metaData = metaData;
}
@Override
public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.product_list_layout, parent, false);
holder.libelle_produit = (TextView) convertView.findViewById(R.id.libelle_produit);
holder.description_short = (TextView) convertView.findViewById(R.id.description_du_produit);
holder.prix_ttc = (TextView) convertView.findViewById(R.id.prix_ttc);
holder.product_image = (ImageView) convertView.findViewById(R.id.product_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.libelle_produit.setText(getLibelleProduit());
holder.description_short.setText(getDescriptionShort());
holder.prix_ttc.setText(getPrixTtc());
// int size = bitmapCached.size();
// if(size!=0){
// holder.img2.setImageBitmap(bitmapCached.get(0));
// }
//
// ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(), holder.img, options, new AnimateFirstDisplayListener());
return convertView;
}
static class ViewHolder {
TextView libelle_produit;
TextView prix_ttc;
TextView description_short;
ImageView product_image;
}
} |
Mon interface:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| package com.example.productrobospice.model;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public interface Item {
public View getView(LayoutInflater inflater, View convertView, ViewGroup parent);
} |
J'ai cherché longuement sur internet et j'ai paramétrer le Builder de Gson comme ceci
Code:
1 2
| Gson gson = new GsonBuilder().registerTypeAdapter(Product.class, new ProductInstanceCreator()).setPrettyPrinting().create() ;
return super.createRestAdapterBuilder().setConverter(new GsonConverter(gson)); |
Code:
1 2 3 4 5 6 7
| public class ProductInstanceCreator implements InstanceCreator<Item> {
@Override
public Item createInstance(Type arg0) {
return new Product();
}
} |
Mais j'ai toujours la même erreur. Avez vous des pistes svp ?
Merci