我制作了一个简单的移动(Android)应用和网络(Play 2.1.0)应用。这实际上是Android上的一个傻瓜游戏,你可以在网络应用上发布你的分数(当然还要获得前10名的分数)。
因此,GET最高分数请求和POST请求都在本地(在我的计算机上)完美无瑕地工作。但是当我在Heroku上推送我的应用程序时,只有GET最高分要求工作。正如我所注意到的,POST请求的问题在于接收JSON的web api(Play2)控制器(来自Android)。我想它不知道如何从请求中解析Json ......
我使用这种方式处理Json请求:http://www.playframework.com/documentation/2.1.0/JavaJsonRequests
我修改了我的代码以找到问题:
@BodyParser.Of(BodyParser.Json.class)
public static Result newScore() {
JsonNode json;
try {
json = request().body().asJson();
String jsonString = json.toString();
return ok(jsonString);
} catch (Exception e1) {
return ok("PROBLEM");
}
}
localhost上的响应是我发送的json字符串。 Heroku上的响应是使用String PROBLEM捕获的异常。这是一个非常奇怪的问题,我不知道为什么它在localhost上运行正常,但在heroku上却没有...任何想法? :)
UPDATE(Android logcat):
02-08 00:01:09.140: I/SendResultService(18709): <!DOCTYPE html>
02-08 00:01:09.140: I/SendResultService(18709): <html>
02-08 00:01:09.140: I/SendResultService(18709): <head>
02-08 00:01:09.140: I/SendResultService(18709): <title>Error</title>
02-08 00:01:09.140: I/SendResultService(18709): <link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAlFJREFUeNqUU8tOFEEUPVVdNV3dPe8xYRBnjGhmBgKjKzCIiQvBoIaNbly5Z+PSv3Aj7DSiP2B0rwkLGVdGgxITSCRIJGSMEQWZR3eVt5sEFBgTb/dN1yvnnHtPNTPG4PqdHgCMXnPRSZrpSuH8vUJu4DE4rYHDGAZDX62BZttHqTiIayM3gGiXQsgYLEvATaqxU+dy1U13YXapXptpNHY8iwn8KyIAzm1KBdtRZWErpI5lEWTXp5Z/vHpZ3/wyKKwYGGOdAYwR0EZwoezTYApBEIObyELl/aE1/83cp40Pt5mxqCKrE4Ck+mVWKKcI5tA8BLEhRBKJLjez6a7MLq7XZtp+yyOawwCBtkiBVZDKzRk4NN7NQBMYPHiZDFhXY+p9ff7F961vVcnl4R5I2ykJ5XFN7Ab7Gc61VoipNBKF+PDyztu5lfrSLT/wIwCxq0CAGtXHZTzqR2jtwQiXONma6hHpj9sLT7YaPxfTXuZdBGA02Wi7FS48YiTfj+i2NhqtdhP5RC8mh2/Op7y0v6eAcWVLFT8D7kWX5S9mepp+C450MV6aWL1cGnvkxbwHtLW2B9AOkLeUd9KEDuh9fl/7CEj7YH5g+3r/lWfF9In7tPz6T4IIwBJOr1SJyIGQMZQbsh5P9uBq5VJtqHh2mo49pdw5WFoEwKWqWHacaWOjQXWGcifKo6vj5RGS6zykI587XeUIQDqJSmAp+lE4qt19W5P9o8+Lma5DcjsC8JiT607lMVkdqQ0Vyh3lHhmh52tfNy78ajXv0rgYzv8nfwswANuk+7sD/Q0aAAAAAElFTkSuQmCC">
02-08 00:01:09.140: I/SendResultService(18709): <style>
02-08 00:01:09.140: I/SendResultService(18709): html, body, pre {
02-08 00:01:09.140: I/SendResultService(18709): margin: 0;
02-08 00:01:09.140: I/SendResultService(18709): padding: 0;
02-08 00:01:09.140: I/SendResultService(18709): font-family: Monaco, 'Lucida Console', monospace;
02-08 00:01:09.140: I/SendResultService(18709): background: #ECECEC;
02-08 00:01:09.140: I/SendResultService(18709): }
02-08 00:01:09.140: I/SendResultService(18709): h1 {
02-08 00:01:09.140: I/SendResultService(18709): margin: 0;
02-08 00:01:09.140: I/SendResultService(18709): background: #A31012;
02-08 00:01:09.140: I/SendResultService(18709): padding: 20px 45px;
02-08 00:01:09.140: I/SendResultService(18709): color: #fff;
02-08 00:01:09.140: I/SendResultService(18709): text-shadow: 1px 1px 1px rgba(0,0,0,.3);
02-08 00:01:09.140: I/SendResultService(18709): border-bottom: 1px solid #690000;
02-08 00:01:09.140: I/SendResultService(18709): font-size: 28px;
02-08 00:01:09.140: I/SendResultService(18709): }
02-08 00:01:09.140: I/SendResultService(18709): p#detail {
02-08 00:01:09.140: I/SendResultService(18709): margin: 0;
02-08 00:01:09.140: I/SendResultService(18709): padding: 15px 45px;
02-08 00:01:09.140: I/SendResultService(18709): background: #F5A0A0;
02-08 00:01:09.140: I/SendResultService(18709): border-top: 4px solid #D36D6D;
02-08 00:01:09.140: I/SendResultService(18709): color: #730000;
02-08 00:01:09.140: I/SendResultService(18709): text-shadow: 1px 1px 1px rgba(255,255,255,.3);
02-08 00:01:09.140: I/SendResultService(18709): font-size: 14px;
02-08 00:01:09.140: I/SendResultService(18709): border-bottom: 1px solid #BA7A7A;
02-08 00:01:09.140: I/SendResultService(18709): }
02-08 00:01:09.140: I/SendResultService(18709): </style>
02-08 00:01:09.140: I/SendResultService(18709): </head>
02-08 00:01:09.140: I/SendResultService(18709): <body>
02-08 00:01:09.140: I/SendResultService(18709): <h1>Oops, an error occured</h1>
02-08 00:01:09.140: I/SendResultService(18709): <p id="detail">
02-08 00:01:09.140: I/SendResultService(18709): This exception has been logged with id <strong>6d9a6k11h</strong>.
02-08 00:01:09.140: I/SendResultService(18709): </p>
02-08 00:01:09.140: I/SendResultService(18709): </body>
02-08 00:01:09.140: I/SendResultService(18709): </html>
解: 好的,我解决了一个问题 - 这实际上是我的Android代码中的一个问题:
public void postData(String url,JSONObject obj) {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url.toString());
httppost.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(obj.toString());
//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); // <-- PROBLEM
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i(TAG, temp);
}
catch (ClientProtocolException e) {Log.e(TAG, "Client protocol ex");}
catch (IOException e) {Log.e(TAG, "io ex");}
}
此代码现在有效,我评论了有问题的部分:将BasicHeader设置为字符串实体。对于localhost来说,这显然不是问题,但它适用于Heroku。