3

I am trying to use the Parse.com REST API in Java. I have gone through the 4 solutions given here https://parse.com/docs/api_libraries and have selected Parse4J. After importing the source into Netbeans, along with importing the following libraries (which Netbeans told me I needed):

org.slf4j:slf4j-api:jar:1.6.1
org.apache.httpcomponents:httpclient:jar:4.3.2
org.apache.httpcomponents:httpcore:jar:4.3.1
org.json:json:jar:20131018
commons-codec:commons-codec:jar:1.9
junit:junit:jar:4.11
ch.qos.logback:logback-classic:jar:0.9.28
ch.qos.logback:logback-core:jar:0.9.28

I ran the example code from https://github.com/thiagolocatelli/parse4j

Parse.initialize(APP_ID, APP_REST_API_ID); // I replaced these with mine

ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.save();

And I got that it was missing org.apache.commons.logging, so I downloaded that and imported it. Then I ran the code again and got

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:120)
at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:122)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:131)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:193)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.parse4j.command.ParseCommand.perform(ParseCommand.java:44)
at org.parse4j.ParseObject.save(ParseObject.java:450)

I could probably fix this with another import, but I suppose then something else would pop up. I tried the other libraries with similar results, missing a bunch of libraries. Has anyone actually used the Parse.com REST API successfully in Java? If so, I would be grateful if you shared which library/s you used and anything else required to get it going successfully.

Please do not suggest anything you have not tried. It will probably get me going in circles again.

I am using Netbeans.

Thanks.

10
  • 2
    REST APIs are just HTTP calls (and yes, I use them from Java). Your logger isn't setup (so you haven't actually got useful messaging yet). I would recommend you use a tool like maven or gradle or sbt or ivy to manage your dependencies. Commented May 31, 2014 at 3:49
  • Thanks Elliott. I was referring to the API for Parse.com. Have you used this / does this function in the same way? Commented May 31, 2014 at 3:54
  • I haven't used the Parse.com API, but I can tell your logs aren't setup (that's the Exception you have posted anyway). Have you tried using a debugger? You mentioned Netbeans, so here's one way. Commented May 31, 2014 at 3:59
  • Don't think I've ever used one. Is there one built-in to Netbeans? Commented May 31, 2014 at 4:01
  • You say my logs aren't setup. What should I be looking for when using the debugger to fix this? Commented May 31, 2014 at 4:05

1 Answer 1

2

You may have the parse docs confused. Your title says "Rest API" . Yet , your code's from the parse ANDROID SDK!

  1. figure out what interface at parse you will use and then use it ( ANDROID OR REST )

  2. dont mix Android code into a Java/HTTP/REST client to parse.com

  3. Practice some of the curl CLI examples out of the REST API manual and you will know what sequence ( put them into a shell script ) of native command line http/curl expressions you want to submit to parse.

  4. learn how to implement native httpclient in your java . I would suggest some kind of async callbacks

  5. transfer the sequence of Curl command line calls on parse.com into your java process and you will get the same result.

  6. You must be able to log WIRE/ HEADERS with http in order to develope in this environment.

sample code from android _ modified quickly to be near compatible on pure java:

//runnable inside the httpclient's implementation of 'exec'
    public void run() {
        int httprc = 999;
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(YourConnectionMgr.getInstance())
                .addInterceptorLast(new HttpRequestInterceptor() {
                    public void process(
                            final HttpRequest request, 
                            final HttpContext context) throws HttpException, IOException { 

                            if(request.getRequestLine().getMethod() == "GET"){                                                                                                  
                                    request.addHeader("X-Parse-Application-Id", ParseApplication.key_appId);
                                    request.addHeader("X-Parse-REST-API-Key", ParseApplication.key_rest);
                                    request.addHeader("X-Parse-Session-Token",prefs.getString("default_parse_user_token", ""));
                                }

                    }
                })
                .build();   

        try {                       
            HttpResponse response = null;
            switch (method) {
            case GET:
                HttpGet httpGet = new HttpGet(url);
                httpGet.setProtocolVersion(new ProtocolVersion("HTTP", 1,1));
                httpGet.setConfig(this.config);
                response = httpClient.execute(httpGet, context);
                break; }


        } catch (Exception e) {
            handler.sendMessage(Message.obtain(handler,
                    HttpConnection.DID_ERROR, e));
        }  
Sign up to request clarification or add additional context in comments.

2 Comments

OP said he is using Parse4J... You can verify this in the stacktrace: org.parse4j.ParseObject.save(ParseObject.java:450) Parse4J uses very similar naming as the Android API. Parse4J works just fine, as long as the classpath is correct. I don't really see how this answers the question.
This is not the answer to this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.