2012-08-29 55 views
0

我正在爲我的AndroidApp編寫一個RESTful客戶端。我的休息webservice返回給我一個JSON,我用springfreamwork將它分解成java類成員。這樣我的代碼就可以了。 我需要將參數從主要活動傳遞到另一個參與者,所以我按照指導原則將該類(Clinica.class見下文)作爲PARCELABLE實現。現在,應用程序將返回我這個錯誤org.codehaus.jackson.map.JsonMappingException:沒有找到合適的構造函數類型

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class it.stasbranger.clinigomobile.model.Clinica]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: [email protected]; line: 1, column: 3]; nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class it.stasbranger.clinigomobile.model.Clinica]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: [email protected]; line: 1, column: 3] 

這是我Clinica.class

public class Clinica implements Parcelable { 

     @JsonProperty 
     private Integer idclinica; 
     @JsonProperty 
     private String nome; 
     @JsonProperty 
     private Long dataRegistrazione; 
     @JsonProperty 
     private Long version; 
     @JsonProperty 
     private String referente; 
     @JsonProperty 
     private String indirizzo; 
     @JsonProperty 
     private String cap; 
     @JsonProperty 
     private String telefono; 
     @JsonProperty 
     private String email; 
     @JsonProperty 
     private String sitoWeb; 
     @JsonProperty 
     private Boolean abilitata; 
     @JsonProperty 
     private Integer valutazione; 
     @JsonProperty 
     private Double rank; 
     @JsonProperty 
     private String nomeFatturazione; 

     //getters and setters 
      ....... 

     public Clinica (Parcel p){ 
      boolean[] booleans = new boolean[1]; 

      this.cap=p.readString(); 
      this.email=p.readString(); 
      this.indirizzo=p.readString(); 
      this.nome=p.readString(); 
      this.nomeFatturazione=p.readString(); 
      this.referente=p.readString(); 
      this.sitoWeb=p.readString(); 
      this.telefono=p.readString(); 

      this.idclinica=p.readInt(); 
      this.valutazione=p.readInt(); 

      this.dataRegistrazione=p.readLong(); 
      this.version=p.readLong(); 

      this.rank=p.readDouble(); 

      p.readBooleanArray(booleans); 
      this.abilitata=booleans[0]; 
     } 

     public int describeContents() { 
      return 0; 
     } 

     public void writeToParcel(Parcel dest, int flags) { 
      boolean[] booleans = new boolean[1]; 
      Arrays.fill(booleans, abilitata); 
      dest.writeString(cap); 
      dest.writeString(email); 
      dest.writeString(indirizzo); 
      dest.writeString(nome); 
      dest.writeString(nomeFatturazione); 
      dest.writeString(referente); 
      dest.writeString(sitoWeb); 
      dest.writeString(telefono); 
      dest.writeInt(idclinica); 
      dest.writeInt(valutazione); 
      dest.writeLong(dataRegistrazione); 
      dest.writeLong(version); 
      dest.writeDouble(rank); 
      dest.writeBooleanArray(booleans); 
     } 

     public static final Parcelable.Creator<Clinica> CREATOR = new Creator<Clinica>() { 

      public Clinica[] newArray(int size) { 
       return new Clinica[size]; 
      } 

      public Clinica createFromParcel(Parcel source) { 
       return new Clinica(source); 
      } 
     }; 

    } 

,這是我的異步調用,使請求

...... 
Clinica data[] = restTemplate.getForObject(urls[0], Clinica[].class, vars); 

有什麼建議?提前致謝。

回答

3

答案很簡單:刪除構造函數(同樣在父類中,如果裏面有任何東西),一切都會工作!也許有像@JsonIgnore這樣的註釋,但它不適用於構造函數。

+0

你能幫我知道,爲什麼我們需要刪除參數構造函數或添加無參數的構造函數? –

+0

呃,這很長一段時間,所以我可以猜測它:jackson增加了自己的構造函數,並且由於你定義了一個構造函數,它不知道使用哪一個構造函數。看看[這篇文章](http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html)。 – shmoula

相關問題