gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:25 25 4
gpt4 key购买 nike

我正在尝试在 Java 中使用 Parse.com REST API。我已经完成了这里给出的 4 个解决方案 https://parse.com/docs/api_libraries并选择了 Parse4J。将源代码导入 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

我运行了来自 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();

我发现它缺少 org.apache.commons.logging,所以我下载并导入了它。然后我再次运行代码得到了

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)

我可能可以通过另一个导入来解决这个问题,但我想其他东西会弹出。我尝试了其他具有类似结果的库,但缺少一堆库。有没有人实际在 Java 中成功使用过 Parse.com REST API?如果是这样,如果您能分享您使用的库以及成功运行所需的任何其他内容,我将不胜感激。

请不要推荐任何你没有尝试过的东西。这可能会让我再次陷入困境。

我正在使用 Netbeans。

谢谢。

最佳答案

您可能对解析文档感到困惑。您的标题是“Rest API”。然而,您的代码来自解析 ANDROID SDK!

  1. 找出您将使用的解析界面,然后使用它(ANDROID 或 REST)

  2. 不要将 Android 代码混入 Java/HTTP/REST 客户端以解析.com

  3. 练习 REST API 手册中的一些 curl CLI 示例,您将知道要提交以供解析的 native 命令行 http/curl 表达式的顺序(将它们放入 shell 脚本中)。

  4. 了解如何在您的 java 中实现 native httpclient。我会建议某种 async callbacks

  5. 将 parse.com 上的 Curl 命令行调用序列转移到您的 java 进程中,您将获得相同的结果。

  6. 您必须能够使用 http 登录 WIRE/HEADERS 才能在此环境中进行开发。

来自 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));
}

关于java - Java 中的 Parse.com REST API(非 Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com