lundi 27 avril 2020

Retrofit 2: Extract Value from Response Array

Im trying to extract a value from the response body of my Get Call in retrofit 2. I want to extract evey "id" yet I cant call the getters from the Response class, i think it could have something to do with the [] in the response class, when i remove the [] its returns a jackson error so i seem to be stuck

Call:

public interface RetrofitCallsGetFavourites {


@GET("diagnosis/configuration")
Call<GetFavouritesReponse[]> getDiagnosisFavourites (@Query("favourite") Boolean fave, 
@Header("Authorization") String authHeader);

}

This is the method im using, it wont let me call any getters

public class LoginApiStepDefinition extends TestBaseFix {

Retrofit retrofit = super.buildRetrofit(super.buildOkHttpClient());
RetrofitCallsLogin call = retrofit.create(RetrofitCallsLogin.class);
RetrofitCallsGetFavourites favecall = retrofit.create(RetrofitCallsGetFavourites.class);





@Then("I get the list of favourites with {string} and {string}")
public void iGetTheListOfFavouritesWithAnd(String username, String password) throws Exception {
    String auth = "Bearer " + LoginApi(username,password);
    String list = favecall.getDiagnosisFavourites(true,auth).execute().body().toString();
    System.out.println(list);

}
}

Response body class

public class GetFavouritesReponse {

@JsonProperty("id")
private Integer id;
@JsonProperty("version")
private Integer version;
@JsonProperty("createdDate")
private Object createdDate;
@JsonProperty("modifiedDate")
private Object modifiedDate;
@JsonProperty("rie")
private Boolean rie;
@JsonProperty("diagnosisName")
private String diagnosisName;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("id")
public Integer getId() {
    return id;
}

@JsonProperty("id")
public void setId(Integer id) {
    this.id = id;
}

@JsonProperty("version")
public Integer getVersion() {
    return version;
}

@JsonProperty("version")
public void setVersion(Integer version) {
    this.version = version;
}

@JsonProperty("createdDate")
public Object getCreatedDate() {
    return createdDate;
}

@JsonProperty("createdDate")
public void setCreatedDate(Object createdDate) {
    this.createdDate = createdDate;
}

@JsonProperty("modifiedDate")
public Object getModifiedDate() {
    return modifiedDate;
}

@JsonProperty("modifiedDate")
public void setModifiedDate(Object modifiedDate) {
    this.modifiedDate = modifiedDate;
}

@JsonProperty("rie")
public Boolean getRie() {
    return rie;
}

@JsonProperty("rie")
public void setRie(Boolean rie) {
    this.rie = rie;
}

@JsonProperty("diagnosisName")
public String getDiagnosisName() {
    return diagnosisName;
}

@JsonProperty("diagnosisName")
public void setDiagnosisName(String diagnosisName) {
    this.diagnosisName = diagnosisName;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}


}

Here is an example of what is returned, the call is working correctly i just cant parse the informaiton correctly, im trying to extract all the id's

[ {
"id" : 15,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Angina pectoris - Angina pectoris with documented spasm"
}, {
"id" : 14,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Angina pectoris - Unstable angina"
},{
"id" : 22,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Seropositive rheumatoid arthritis - Seropositive rheumatoid arthritis, unspecified                 
Pelvic region and thigh"
}, {
"id" : 27,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Test Diagnosis Cardiology"
}, {
"id" : 26,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Test Diagnosis with Service"
} ]

Aucun commentaire:

Enregistrer un commentaire