我正在尝试使用 HttpsUrlConnection 通过 Parse 的 REST API 在我的 Parse.com 数据库中创建一个新对象。他们的 REST API 只接受 JSON。我已经让一切正常工作,数据库将接受新的对象条目——除了我试图包含日期字段的时候。当我传入日期时,服务器完全拒绝该对象。
我在他们的文档中找到了这个关于在使用 REST API 时如何在对象中添加日期字段的描述:
The Parse mobile client libraries also support dates, binary data, and relational data. In the REST API, these values are encoded as JSON hashes with the __type field set to indicate their type, so you can read or write these fields if you use the correct encoding.
The Date type contains a field iso which contains a UTC timestamp stored in ISO 8601 format with millisecond precision: 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)
吗?
关于Java:通过 Parse.com REST API 将日期对象作为 JSONObject 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201814/