簡體   English   中英

Java中帶有Parse.com請求的無效JSON

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM