问题
I would like to be able to use the Parse4J library (https://github.com/thiagolocatelli/parse4j/) to connect to a hosted Parse-Server. I want to use https://parseapi.back4app.com as an API endpoint. They provide a solid Parse hosting solution.
Code:
import org.parse4j.Parse;
import org.parse4j.ParseException;
import org.parse4j.ParseObject;
import org.parse4j.ParseQuery;
import org.parse4j.callback.GetCallback;
/**
* Created by Martin on 3/27/2017.
*/
public class Parse4JStarter {
public static void main(String[] args) {
Parse.initialize("applicationId","restAPIKey");
if (Parse.getApplicationId() != null) {
System.out.println("ParseConnection successful! " + Parse.getApplicationId());
try {
queryPost();
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void queryPost() throws Exception {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
query.getInBackground("4tD9TIIIhv", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
System.out.println("ParseObject printed successfully! " + object);
} else {
e.printStackTrace();
}
}
});
}
}
Dependencies:
<dependencies>
<dependency>
<groupId>com.github.thiagolocatelli</groupId>
<artifactId>parse4j</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
I tried overriding the Maven repository by supplying a local version, i.e. 1.4-FIXED
and changing the API_ENDPOINT value in ParseConstants.java to point to https://parseapi.back4app.com
, which is where my database is hosted:
public class ParseConstants {
public static final String API_ENDPOINT = "https://parseapi.back4app.com";
public static final String API_VERSION = "1";
public static final String HEADER_CONTENT_TYPE = "Content-Type";
public static final String HEADER_APPLICATION_ID = "X-Parse-Application-Id";
public static final String HEADER_REST_API_KEY = "X-Parse-REST-API-Key";
public static final String HEADER_MASTER_KEY = "X-Parse-Master-Key";
public static final String HEADER_SESSION_TOKEN = "X-Parse-Session-Token";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final String FIELD_OBJECT_ID = "objectId";
public static final String FIELD_CREATED_AT = "createdAt";
public static final String FIELD_UPDATED_AT = "updatedAt";
public static final String FIELD_SESSION_TOKEN = "sessionToken";
public static int MAX_PARSE_FILE_SIZE = 10485760;
}
I received an ParseException:
ParseException [code=109, error=unauthorized]
at org.parse4j.command.ParseResponse.getParseError(ParseResponse.java:122)
at org.parse4j.command.ParseResponse.getException(ParseResponse.java:78)
at org.parse4j.ParseQuery.find(ParseQuery.java:598)
at org.parse4j.ParseQuery.find(ParseQuery.java:469)
at org.parse4j.ParseQuery.get(ParseQuery.java:377)
at org.parse4j.ParseQuery$GetInBackgroundThread.run(ParseQuery.java:452)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
This means that my connection was not properly initialized:
/**
* You must call Parse.initialize before using the Parse library.
*/
public static final int NOT_INITIALIZED = 109;
There is probably more that I can do by investigating the Parse4J library more thoroughly. At this point, however, I'm lost. What can I do?
回答1:
Thanks to the developers at Parse4J, I was able to solve my problem by updating my dependency to the latest snapshot version 1.5-SNAPSHOT
and then updating my initialization method to include a third parameter that holds the custom API endpoint. This was possible some 8 months ago but the documentation does not mention anything about using the latest build, since it's not an official release yet. I got this working using https://parseapi.back4app.com. They provide a solid Parse hosting solution but you can replace this with your own URI. A new release should be around the corner I assume...
<dependencies>
<dependency>
<groupId>com.github.thiagolocatelli</groupId>
<artifactId>parse4j</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
Initialization method:
Parse.initialize(applicationId, restAPIKey, "https://parseapi.back4app.com");
See documentation here: https://github.com/thiagolocatelli/parse4j/commit/4113b422631c364488bc51152da8b071542e8159
来源:https://stackoverflow.com/questions/43110992/how-can-i-connect-to-a-hosted-parse-server-over-parse4j