0

我制作了一个简单的移动(Android)应用程序和网络(Play 2.1.0)应用程序。它实际上是 Android 上的打地鼠游戏,您可以在 Web 应用程序上发布您的分数(当然还可以获取前 10 名分数的列表)。

因此,GET 最高分请求和 POST 请求都可以在本地(在我的计算机上)完美运行。但是当我在 Heroku 上推送我的应用程序时,只有 GET 最高分请求有效。正如我所注意到的,POST 请求的问题在于接收 JSON(来自 Android)的 web api (Play2) 控制器。我猜它不知道如何从请求中解析 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 上却不行……有什么想法吗?:)

更新(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 的问题。

4

0 回答 0