thiagolocatelli / parse4j

Java Library to deal with Parse (parse.com) REST API
http://thiagolocatelli.github.io/parse4j
143 stars 117 forks source link

How can I connect to a hosted Parse-Server over Parse4J? #81

Open SeloSlav opened 7 years ago

SeloSlav commented 7 years ago

Many people have been asking how to do this so I thought I'd just post some sample code with a working solution. Could we sticky this, or at least update the main README? I got this working using https://parseapi.back4app.com as an API endpoint. They provide a solid Parse hosting solution. This is just a proof of concept in a single Java class. I'm sure it would be more useful in a Spring or Springboot application.

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", "https://parseapi.back4app.com");

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

Make sure your dependency includes the latest snapshot build, 1.5-SNAPSHOT. I also included some logging dependencies because my console told me to include them for some reason:

<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>
cheetah90 commented 7 years ago

Hi @santafebound ! Thanks for the sample code. A quick question: since parse4j 1.5-SNAPSHOT does not seem to exist in any public repo, do we have to build the jar locally and put it in the local maven repo ourselves? Thanks Allen

SeloSlav commented 7 years ago

Yes that's exactly what I did :)

On Jul 8, 2017 10:06 PM, "Allen Lin" notifications@github.com wrote:

Hi @santafebound https://github.com/santafebound ! Thanks for the sample code. A quick question: since parse4j 1.5-SNAPSHOT does not seem to exist in any public repo, do we have to build the jar locally and put it in the local maven repo ourselves? Thanks Allen

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/81#issuecomment-313899146, or mute the thread https://github.com/notifications/unsubscribe-auth/AIa4FEsYuiQbVr61ryfY9cOYV78s52nEks5sMF_ygaJpZM4Mr1en .