Java: object sending data as JSONObject via Parse.com REST API

I am trying to create a new object in my Parse.com database using the Parse REST API using HttpsUrlConnection. Their REST API accepts only JSON. I got everything to work when the database will accept a new object record - except when I try to include a Date field. When I pass the date, the server completely rejects the object.

I found this in my documentation, this description of how to add a Date field to an object when using the REST API:

Parse Mobile Client Libraries also support dates, binary data, and relational data. In the REST API, these values ​​are encoded as JSON hashes with a given __type field indicating their type, so you can read or write these fields if you use the correct encoding.

The Date type contains the iso field, which contains the UTC timestamp stored in ISO 8601 format, accurate to the millisecond: YYYY-MM-DDT: MM: SS.MMMZ.

{ "__type": "Date", "iso": "2011-08-21T18:02:52.249Z" } 

So, if I want to create a new object and pass it to the database, I assume that I need to create another JSONObject and pass it to the appropriate field. However, when I tried it, he still rejected it. Below I tried to add a Date object as a parameter to the transmission stored in my own JSONObject. What am I doing wrong? What is the correct way to send a Date object to JSON based on their documents?

 //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 REST API jsonParam.put("sendTime", dateAsObj); 

Here is the complete function that makes an HTTP request for context and link:

 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()); } 

Any help or input would be appreciated.

+6
source share
2 answers

Your format does not match the one they need. For instance:

 Theirs: 2011-08-21T18:02:52.249Z Yours: 2011-08-21 18:02:52.249 

You miss T and Z

So try changing the format:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 

Honestly, I would be surprised if this were not handled automatically, though ... did you just dateAsObj.put("iso", sendTime) ?

+4
source

The standard Date object is not stored in Parse. You must set it as a JSON object with "__type": "Date" and "iso": Date_String_you_want_to_set . with a date string formatted as follows:

 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
+2
source

Source: https://habr.com/ru/post/973536/


All Articles