简体   繁体   English

Java中的Parse.com REST API(非Android)

[英]Parse.com REST API in Java (NOT Android)

I am trying to use the Parse.com REST API in Java. 我试图在Java中使用Parse.com REST API。 I have gone through the 4 solutions given here https://parse.com/docs/api_libraries and have selected Parse4J. 我已经完成了这里给出的4个解决方案https://parse.com/docs/api_libraries并选择了Parse4J。 After importing the source into Netbeans, along with importing the following libraries (which Netbeans told me I needed): 将源导入Netbeans后,导入以下库(Netbeans告诉我,我需要):

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 我从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. 我得知它缺少org.apache.commons.logging,所以我下载并导入它。 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? 有没有人在Java中成功使用过Parse.com REST API? 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. 我正在使用Netbeans。

Thanks. 谢谢。

You may have the parse docs confused. 您可能会将解析文档混淆。 Your title says "Rest API" . 你的标题是“Rest API”。 Yet , your code's from the parse ANDROID SDK! 然而,您的代码来自解析ANDROID SDK!

  1. figure out what interface at parse you will use and then use it ( ANDROID OR REST ) 找出你将使用的解析界面,然后使用它(ANDROID OR REST)

  2. dont mix Android code into a Java/HTTP/REST client to parse.com 不要将Android代码混合到Java / HTTP / REST客户端到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. 从REST API手册中练习一些curl CLI示例,您将知道要提交解析的本机命令行http / curl表达式的序列(将它们放入shell脚本中)。

  4. learn how to implement native httpclient in your java . 学习如何在java中实现本机httpclient。 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. 将parse.com上的Curl命令行调用序列传递到您的java进程中,您将得到相同的结果。

  6. You must be able to log WIRE/ HEADERS with http in order to develope in this environment. 您必须能够使用http记录WIRE / HEADERS才能在此环境中进行开发。

sample code from android _ modified quickly to be near compatible on pure java: 来自android _的示例代码快速修改为在纯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));
        }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM