我有下面的curl命令可以很好地從我的Parse.com項目中檢索json對象列表。如何生成包含JSON的URL作爲GET操作的參數
curl -X GET -H "X-Parse-Application-Id: {MY_APP_ID}" -H "X-Parse-REST-API-Key: {MY_REST_API}" -H "X-Parse-Session-Token: {MY_SESSION_TOKEN}" -G --data-urlencode "where={\"$relatedTo\":{\"object\":{\"__type\":\"Pointer\",\"className\":\"_Role\",\"objectId\":\"{MY_OBJECT_ID}\"},\"key\":\"users\"}}" https://api.parse.com/1/users
現在我想創建一個Java Servlet與HttpURLConnection做同樣的事情。我創建了以下網址:
String url = "https://api.parse.com/1/users?where={\"$relatedTo\":{\"object\":{\"__type\":\"Pointer\",\"className\":\"_Role\",\"objectId\":\"{MY_OBJECT_ID}\"},\"key\":\"users\"}}"
URL object = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) object
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("X-Parse-Application-Id", APP_ID);
connection.setRequestProperty("X-Parse-REST-API-Key", REST_API_KEY);
connection.setRequestProperty("X-Parse-Session-Token", sessionToken);
connection.setRequestProperty("Content-Type", "application/json");
然後我結束了得到一個錯誤:
java.lang.IllegalArgumentException: Illegal character in query at index 36: https://api.parse.com/1/users?where={"$relatedTo":{"object":{"__type":"Pointer","className":"_Role","objectId":"{MY_OBJECT_ID}"},"key":"users"}}
我也有點相信它是拋出了錯誤的URL。但不知道如何解決它。謝謝!
什麼是'str'以及它如何與'strUrl'相關?你也需要';'字符串文字後 –