简体   繁体   English

Java中带有Parse.com请求的无效JSON

[英]Invalid JSON with Parse.com Request in Java

I'm trying to copy Parse's Curl REST API code into Java as shown in their documentation here : 我试图解析的卷曲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

Essentially, I'm trying to find a user's item in table Items based upon the selected user's objectid. 本质上,我试图根据所选用户的objectid在表Items中找到用户的项目。

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. 目前,我只是通过将程序作为Java应用程序运行来进行测试,但是从响应中收到了{“ code”:107,“ error”:“ invalid JSON”}错误。 I just can't seem to get the data-urlencode of the curl correct (or it could be I'm completely doing it wrong). 我只是似乎无法正确获取curl的数据urlencode(或者可能是我完全做错了)。 If it matters, I'm using OkHttp. 如果有关系,我正在使用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. getUserId()方法可以正常工作,并返回正确的用户ID字符串。 Any help would be appreciated. 任何帮助,将不胜感激。

I don't think you've understood what the "--data-urlencode" option does to the curl request. 我认为您不了解“ --data-urlencode”选项对curl请求的作用。 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. 如文档中所指定,这是一个GET请求,看起来您正在尝试发送POST请求。 Have you tried to send a GET-request with the data url-encoded? 您是否尝试过发送带有url编码数据的GET请求?

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

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