我正在尝试使用 HttpsUrlConnection 通过 Parse 的 REST API 在 Parse.com 数据库中创建一个新对象。他们的 REST API 只接受 JSON。我已经让一切正常工作,数据库将接受新的对象条目 - 除非我尝试包含日期字段。当我传入日期时,服务器完全拒绝该对象。
我在他们的文档中找到了有关如何在使用 REST API 时在对象中添加日期字段的描述:
Parse 移动客户端库还支持日期、二进制数据和关系数据。在 REST API 中,这些值被编码为 JSON 哈希,并设置 __type 字段来指示其类型,因此如果您使用正确的编码,则可以读取或写入这些字段。
Date 类型包含一个字段 iso,其中包含以 ISO 8601 格式存储的 UTC 时间戳,精度为毫秒:YYYY-MM-DDTHH:MM:SS.MMMZ。
{
"__type": "Date",
"iso": "2011-08-21T18:02:52.249Z"
}
因此,如果我想创建一个新对象并将其传递到数据库,我假设我需要创建另一个 JSONObject 并将其传递到其相应的字段中。然而,当我尝试这样做时,它仍然拒绝了。下面是我尝试将 Date 对象添加为要传递的参数,存储在其自己的 JSONObject 中。我究竟做错了什么?根据他们的文档,以 JSON 格式发送 Date 对象的正确方法是什么?
//datePicked is a Calendar object
Date sendTime = datePicked.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String sendTimeInUTC = formatter.format(sendTime);
//Storing the Date object as a JSONObject, as specified
JSONObject dateAsObj = new JSONObject();
dateAsObj.put("__type", "Date");
dateAsObj.put("iso", sendTimeInUTC);
//jsonParam is the JSONObject that is being sent over to Parse's REST API
jsonParam.put("sendTime", dateAsObj);
这是发出 http 请求的完整函数,供上下文和参考:
private void runHttpRequest(final String emailAddress, final String password,
String[] recipients, final String title, final String message) throws MalformedURLException {
//Stores email in Parse DB, from Java servlet
String url = "https://api.parse.com/1/classes/Email";
URL obj = new URL(url);
HttpsURLConnection con = null;
try {
con = (HttpsURLConnection) obj.openConnection();
} catch (IOException e) {
System.out.println("Failed to connect to http link");
e.printStackTrace();
}
//add request header
try {
con.setRequestMethod("POST");
} catch (ProtocolException e) {
System.out.println("Failed to set to POST");
e.printStackTrace();
}
con.setRequestProperty("X-Parse-Application-Id", "*****************");
con.setRequestProperty("X-Parse-REST-API-Key", "*******************");
con.setRequestProperty("Content-Type", "application/json");
JSONObject jsonParam = new JSONObject();
jsonParam.put("username", "******");
jsonParam.put("emailID", 1);
jsonParam.put("universalID", "******");
Gson converter = new Gson();
String recipientsInJson = converter.toJson(recipients);
jsonParam.put("to", recipientsInJson);
jsonParam.put("from", emailAddress);
jsonParam.put("title", title);
jsonParam.put("body", message);
Date sendTime = datePicked.getTime();
//jsonParam.put("sendTime", sendTime);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String sendTimeInUTC = formatter.format(sendTime);
System.out.println("UTC time" + sendTimeInUTC);
JSONObject dateAsObj = new JSONObject();
dateAsObj.put("__type", "Date");
dateAsObj.put("iso", sendTimeInUTC);
System.out.println(dateAsObj.toString());
jsonParam.put("sendTime", dateAsObj);
String urlParameters = jsonParam.toString();
// Send POST request
con.setDoOutput(true);
DataOutputStream wr = null;
try {
wr = new DataOutputStream(con.getOutputStream());
} catch (IOException e1) {
System.out.println("Failed to get output stream");
e1.printStackTrace();
}
try {
wr.writeBytes(urlParameters);
} catch (IOException e) {
System.out.println("Failed to connect to send over Parse object as parameter");
e.printStackTrace();
}
try {
wr.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
wr.close();
} catch (IOException e) {
System.out.println("Failed to connect to close datastream connection");
e.printStackTrace();
}
int responseCode = 0;
try {
responseCode = con.getResponseCode();
} catch (IOException e) {
System.out.println("Failed to connect to get response code");
e.printStackTrace();
}
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
} catch (IOException e) {
System.out.println("Failed to get input stream");
e.printStackTrace();
}
String inputLine;
StringBuffer response = new StringBuffer();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (IOException e) {
System.out.println("Failed to read line");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
System.out.println("Failed to close input stream");
e.printStackTrace();
}
//print result
System.out.println(response.toString());
}
任何帮助或意见将不胜感激。
您的格式与他们要求的格式不符。例如:
Theirs: 2011-08-21T18:02:52.249Z
Yours: 2011-08-21 18:02:52.249
您错过了
T
和 Z
。
所以尝试将格式更改为:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
说实话,如果这没有自动处理,我会感到惊讶 - 你尝试过吗
dateAsObj.put("iso", sendTime)
?
标准日期对象不存储在 Parse 中。您必须使用
"__type": "Date"
和 "iso": Date_String_you_want_to_set
将其设置为 JSON 对象。
日期字符串格式如下:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
java.util
日期时间 API 及其相应的解析/格式化类型 SimpleDateFormat
已过时且容易出错。 2014 年 3 月,现代日期时间 API 取代了该 API。从那时起,强烈建议切换到现代日期时间 API java.time
。
在讨论解决方案之前
在讨论解决方案之前,我们先来了解一下
'Z'
和Z
之间的区别。虽然 'Z'
只是一个字符文字,但 Z
是格式化代码 - 零时区偏移的时区指示符。它代表 Zulu,并指定 UTC 偏移量(+00:00 小时)。检查 'Z'
与 Z
不同以了解更多信息。
解决方案:
java.time
API 有 OffsetDateTime
来表示带有偏移量的日期时间,例如2011-08-21T18:02:52.249Z、2011-08-21T18:02:52.249-05:00 等
演示:
String strDateTime = "2011-08-21T18:02:52.249Z";
OffsetDateTime odt = OffsetDateTime.parse("2011-08-21T18:02:52.249Z");
System.out.println(odt);
输出:
2011-08-21T18:02:52.249Z
java.time
API 与 JDBC。
了解现代日期时间 API