IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Gson comment sérialiser une classe qui implémente une interface ?


Sujet :

Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Gson gson = new GsonBuilder().registerTypeAdapter(Product.class, new ProductInstanceCreator()).setPrettyPrinting().create() ;
    		return super.createRestAdapterBuilder().setConverter(new GsonConverter(gson));
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 582
    Par défaut
    D'après le message d'erreur, ce qu'il n'arrive pas à désérialiser ce n'est pas un Product, c'est un Item.
    Il faut donc lui enregistrer le moyen pour Item.class, pas pour Product.class.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Réponses: 0
    Dernier message: 22/04/2010, 10h41
  2. Trouver classe qui implémente une interface
    Par matdur dans le forum C#
    Réponses: 6
    Dernier message: 25/06/2009, 10h14
  3. éxecuter une classe qui contient une classe annonyme
    Par star-watcher dans le forum Débuter avec Java
    Réponses: 9
    Dernier message: 09/03/2009, 01h26
  4. Réponses: 1
    Dernier message: 26/02/2009, 15h38
  5. Réponses: 27
    Dernier message: 03/01/2008, 11h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo