[英]Invalid JSON with Parse.com Request in Java
我试图解析的卷曲REST API代码复制到Java作为其所示文档中的位置 :
curl -X GET \
-H "X-Parse-Application-Id: APPLICATION-ID" \
-H "X-Parse-REST-API-Key: REST-API-KEY" \
-G \
--data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/GameScore
本质上,我试图根据所选用户的objectid在表Items中找到用户的项目。
目前,我只是通过将程序作为Java应用程序运行来进行测试,但是从响应中收到了{“ code”:107,“ error”:“ invalid JSON”}错误。 我只是似乎无法正确获取curl的数据urlencode(或者可能是我完全做错了)。 如果有关系,我正在使用OkHttp。
这是我的代码如下:
public List<Item> getItemList(String user) {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
String userID = getUserID(user);
Gson gson = new Gson();
System.out.println(userID);
if(userID != null) {
String json = "'where={\"Owner:\"" + userID + "\"}'";
RequestBody reqBody = RequestBody.create(JSON, json );
Request request = new Request.Builder()
.url("https://api.parse.com/1/classes/Items")
.header("Content-type", "application/json")
.post(reqBody)
.addHeader("X-Parse-REST-API-Key", REST_API_KEY)
.addHeader("X-Parse-Application-Id", APPLICATION_ID)
.build();
Response resp;
try {
resp = client.newCall(request).execute();
String html = resp.body().string();
System.out.println("Html is: " + html);
} catch (Exception e) {
}
} else {
return null; //TODO: Throw no user found exception
}
return null;
}
它看起来很丑,但是现在我正试图使其工作。 getUserId()方法可以正常工作,并返回正确的用户ID字符串。 任何帮助,将不胜感激。
我认为您不了解“ --data-urlencode”选项对curl请求的作用。 卷曲行发送的请求看起来像编码版本:
encoded > https://api.parse.com/1/classes/GameScore?where=%7B"playerName"%3A"Sean%20Plott"%2C"c
decoded > https://api.parse.com/1/classes/GameScore?where={"playerName":"Sean Plott","cheatMode":false}
如文档中所指定,这是一个GET请求,看起来您正在尝试发送POST请求。 您是否尝试过发送带有url编码数据的GET请求?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.