JSONObject 未被服务器解析
JSONObject not being parsed by server
我正在构建一个 API,当我发送这样的请求时:
POST http://myserver.dev
{
"id": "1",
"string1": "hello",
"string2": "world"
}
一切都按预期工作,使用 curl
和 Postman 进行测试。但是,使用我的 Android 代码,请求失败:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", "1");
jsonObject.put("string1", "hello");
jsonObject.put("string2", "world");
}
catch (JSONException e)
{
e.printStackTrace();
}
String jsonString = jsonObject.toString();
服务器给我这个错误:
JSON::ParserError (757: unexpected token at '"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}"'):
似乎这个 json 对象没有给我预期的 json 字符串。知道这里发生了什么吗?
来自 logcat 的完整 HTTP 请求在这里:
04-26 15:23:56.468 21013-21669/com.johncorser.s D/Retrofit﹕ ---> HTTP POST http://api.s.com/games
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ X-Authorization: 10155399143045080
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ Content-Length: 95
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ {"nameValuePairs":{"id":"1","string1":"hello","string2":"world"}}
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ ---> END HTTP (95-byte body)
并且来自服务器日志:
2015-04-26T19:26:39.371386+00:00 app[web.1]: Parameters: {"_json"=>"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}", "game"=>{}}
JSON::ParserError (757: unexpected token at '"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}"'):
这最终是 Retrofit 如何在 post 之前序列化对象的结果。
我最终从使用 JSONObject
切换到 HashMap<String, String>
并且没有调用任何 .toString()
方法,Retrofit 负责其余部分。
我正在构建一个 API,当我发送这样的请求时:
POST http://myserver.dev
{
"id": "1",
"string1": "hello",
"string2": "world"
}
一切都按预期工作,使用 curl
和 Postman 进行测试。但是,使用我的 Android 代码,请求失败:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", "1");
jsonObject.put("string1", "hello");
jsonObject.put("string2", "world");
}
catch (JSONException e)
{
e.printStackTrace();
}
String jsonString = jsonObject.toString();
服务器给我这个错误:
JSON::ParserError (757: unexpected token at '"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}"'):
似乎这个 json 对象没有给我预期的 json 字符串。知道这里发生了什么吗?
来自 logcat 的完整 HTTP 请求在这里:
04-26 15:23:56.468 21013-21669/com.johncorser.s D/Retrofit﹕ ---> HTTP POST http://api.s.com/games
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ X-Authorization: 10155399143045080
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ Content-Length: 95
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ {"nameValuePairs":{"id":"1","string1":"hello","string2":"world"}}
04-26 15:23:56.469 21013-21669/com.johncorser.s D/Retrofit﹕ ---> END HTTP (95-byte body)
并且来自服务器日志:
2015-04-26T19:26:39.371386+00:00 app[web.1]: Parameters: {"_json"=>"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}", "game"=>{}}
JSON::ParserError (757: unexpected token at '"{\"id\":\"1\",\"string1\":\"hello\",\"string2\":\"world\"}"'):
这最终是 Retrofit 如何在 post 之前序列化对象的结果。
我最终从使用 JSONObject
切换到 HashMap<String, String>
并且没有调用任何 .toString()
方法,Retrofit 负责其余部分。