I'm trying to copy Parse's Curl REST API code into Java as shown in their documentation here :
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
Essentially, I'm trying to find a user's item in table Items based upon the selected user's objectid.
Currently, I'm just testing by running my program as a Java application but I'm getting a {"code":107,"error":"invalid JSON"} error from the response. I just can't seem to get the data-urlencode of the curl correct (or it could be I'm completely doing it wrong). If it matters, I'm using OkHttp.
Here is my code below:
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;
}
It looks ugly, but right now I'm just trying to get it to work. The getUserId() method works correctly and returns the correct user ID string. Any help would be appreciated.
I don't think you've understood what the "--data-urlencode" option does to the curl request. The request the curl line sends looks something like the encoded version:
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}
This is a GET-request, as specified in the documentation, and it looks like you're trying to send a POST-request. Have you tried to send a GET-request with the data url-encoded?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.