The following is the code,json file and the exception I am getting while executing the code.
//Code
private class HttpRequestTask extends AsyncTask<Void, Void, ShiftPlannerModel[]> {
@Override
protected ShiftPlannerModel[] doInBackground(Void... params) {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<ShiftPlannerModel[]> greeting = restTemplate.getForEntity(URL, ShiftPlannerModel[].class);
Log.i(greeting.getBody().toString(), "doInBackground: ");
return greeting.getBody();
} catch (Exception e) {
Log.e("doInBackgrouExcp", e.getMessage(), e);
}
Below code is for Model object and mapping values:
@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class ShiftPlannerModel {
ShiftPlannerModel(){}
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date shiftPlannerDate;
private String resourceName;
private String shiftName;
public ShiftPlannerModel(Date shiftPlannerDate, String resourceName, String shiftName) {
this.shiftPlannerDate = shiftPlannerDate;
this.resourceName = resourceName;
this.shiftName = shiftName;
}
//setters and getters
public static ArrayList<ShiftPlannerModel> fromJson(JSONArray jsonArray) {
JSONObject businessJson;
ArrayList<ShiftPlannerModel> businesses = new ArrayList<ShiftPlannerModel>(jsonArray.length());
// Process each result in json array, decode and convert to business object
for (int i=0; i < jsonArray.length(); i++) {
try {
businessJson = jsonArray.getJSONObject(i);
} catch (Exception e) {
e.printStackTrace();
continue;
}
ShiftPlannerModel business = ShiftPlannerModel.fromJson(businessJson);
if (business != null) {
businesses.add(business);
}
}
return businesses;
}
// Decodes business json into business model object
public static ShiftPlannerModel fromJson(JSONObject jsonObject) {
ShiftPlannerModel shiftPlannerModel = new ShiftPlannerModel();
// Deserialize json into object fields
try {
// shiftPlannerModel.resource = jsonObject.getString("resourceName");
// shiftPlannerModel.ShiftName = jsonObject.getString("resourceId");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date workingDays = sdf.parse(jsonObject.getString("shiftPlannerDate"));
// shiftPlannerModel.shiftPlannerDate = workingDays;
shiftPlannerModel.setResourceName(jsonObject.getString("resourceName"));
shiftPlannerModel.setShiftName(jsonObject.getString("shiftName"));
shiftPlannerModel.setShiftPlannerDate(workingDays);
} catch (JSONException e) {
e.printStackTrace();
return null;
} catch (ParseException e) {
e.printStackTrace();
}
// Return new object
return shiftPlannerModel;
}
}
//json
[
[
"2018-04-16",
"Elias",
"I"
],
[
"2018-04-16",
"Sithik",
"II"
],
[
"2018-04-17",
"Vijay",
"II"
],
----------------------------
//Exception while reading the above JSON format
04-18 07:07:09.089 31444-31460/? E/doInBackgrouExcp: Could not read JSON: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"])
ThanksAlot Scary Wombat!
My bad I have checked it everything but didn't notice that webservice which I am consuming is not in Json format. I will change webservices so that it will return in json format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.