我试图解析的卷曲REST API代码复制到Java Parse.com请求如图他们的文档here在:无效JSON在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
本质上讲,我试图找到一个用户的项表中的项目基于选定的用户的目标。
目前,我只是通过运行我的程序作为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()方法正常工作并返回正确的用户标识字符串。任何帮助,将不胜感激。
你正在使用哪个requestBuilder实现?从Curl -X POST -d - > post.entity。但是,IMO Curl -X GET -G“编码” - > RequestBuilder.addParameters .... http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/方法/ RequestBuilder.html –