![](/img/trans.png)
[英]HttpMessageNotReadableException: Could not read JSON: Read timed out
[英]HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I'
以下是代碼,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”):無法解析的日期:“ I”(通過參考鏈:com.example.admin.myapplication.ShiftPlannerModel [ “ shiftPlannerDate”]); 嵌套的異常是com.fasterxml.jackson.databind.JsonMappingException:無法解析日期值“ I”(格式:“ yyyy-MM-dd”):不可解析的日期:“ I”(通過參考鏈:com.example.admin)。 myapplication.ShiftPlannerModel [“ shiftPlannerDate”])org.springframework.http.converter.HttpMessageNotReadableException:無法讀取JSON:無法解析日期值“ I”(格式:“ yyyy-MM-dd”):無法解析的日期:“ I” (通過參考鏈:com.example.admin.myapplication.ShiftPlannerModel [“ shiftPlannerDate”]); 嵌套的異常是com.fasterxml.jackson.databind.JsonMappingException:無法解析日期值“ I”(格式:“ yyyy-MM-dd”):不可解析的日期:“ I”(通過參考鏈:com.example.admin)。 myapplication.ShiftPlannerModel [“ shiftPlannerDate”])
感謝很多可怕的袋熊!
不好的我已經檢查了所有內容,但沒有注意到我使用的Web服務不是Json格式。 我將更改Web服務,使其以json格式返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.