REST API Parse.com Java

1.6k Views Asked by At

I am going through REST API GUIDE given by http://www.parse.com

There the doc says about creating objects and storing it in a parse via CURL and Python API calls.

The CURL request for creating object using POST request looks like below:

curl -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
  https://api.parse.com/1/classes/GameScore

I wanted to know how can I send request like this using Java.

2

There are 2 best solutions below

1
On BEST ANSWER

There is a number of Parse.com third-party API libraries:

From https://parse.com/docs/api_libraries

JAVA

  • Almonds — A Java REST API that mimics the Android API.
  • mobile-parse-api — This library implements the REST API of parse.com in java with open interfaces for libgdx and playN.
  • Parse4J — Library for the REST API.
  • ParseFacade — Parse Android SDK alternative.

You might want to evaluate them before making your own interface to parse.com

0
On

There is one issue in Parse.com Almonds libraries though. there is issue storing Date or other similar complex object structures.

I came across the same issue and fixed Almonds library code to resolve the issue. Now I am able to save Date like any other data type.

replace the toJSONObject() method in ParseObject.java (in Almonds library) with the code below:

// Define the following class level static variables
private static final String DATE_CLASS = "java.util.Date";
private static final String DATA_TYPE = "__type";
private static final String DATA_ISO = "iso";

private JSONObject toJSONObject() {
    JSONObject jo = new JSONObject();

    // TODO - Girish Sharma: Extend this code to save other complex data types
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    Object obj = null;
    try {
        for (String key : mData.keySet()) {
            obj = get(key);

            String className = obj.getClass().getName();

            // Switch over the data types
            if (className == DATE_CLASS) {
                JSONObject dateObj = new JSONObject();
                dateObj.put(DATA_TYPE, "Date");
                dateObj.put(DATA_ISO, formatter.format(obj));
                jo.put(key, dateObj);
            }
            else {
                jo.put(key, obj);
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jo;
}