以下是代码,json文件以及执行代码时获得的异常。
//代码
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);
}
下面的代码是Model对象和映射值:
@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"
],
----------------------------
//阅读上述JSON格式时出现异常
04-18 07:07:09.089 31444-31460 /? E / doInBackgrouExcp:无法读取JSON:无法解析日期值'I'(格式:“yyyy-MM-dd”): 无法解释的日期:“我”(通过参考链: com.example.admin.myapplication.ShiftPlannerModel [ “shiftPlannerDate”]); 嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法解析 日期值'I'(格式:“yyyy-MM-dd”):无法解释的日期:“我”(通过 参考链: com.example.admin.myapplication.ShiftPlannerModel [ “shiftPlannerDate”]) org.springframework.http.converter.HttpMessageNotReadableException: 无法读取JSON:无法解析日期值'I'(格式: “yyyy-MM-dd”):无法解释的日期:“我”(通过参考链: com.example.admin.myapplication.ShiftPlannerModel [ “shiftPlannerDate”]); 嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法解析 日期值'I'(格式:“yyyy-MM-dd”):无法解释的日期:“我”(通过 参考链: com.example.admin.myapplication.ShiftPlannerModel [ “shiftPlannerDate”])
答案 0 :(得分:0)
ThanksAlot Scary Wombat!
我的不好我已经检查了一切,但没有注意到我正在消费的网络服务不是Json格式。我将更改webservices,使其以json格式返回。