Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay 2: Json request works locally, but not on Heroku
    primarykey
    data
    text
    <p>I made a simple mobile (Android) app and web (Play 2.1.0) app. It's actually a whack-a-mole game on Android and you can POST your score on web app (and of course GET a list of top 10 scores).</p> <p>So, GET top scores request and POST request both work flawlessly locally (on my computer). But when I push my app on Heroku, only GET top scores request work. The problem with POST request is, as I noticed, in the web api (Play2) controller that is receiving JSON (from Android). I guess it doesn't know how to parse Json from the request...</p> <p>I use this way of handling Json request: <a href="http://www.playframework.com/documentation/2.1.0/JavaJsonRequests" rel="nofollow">http://www.playframework.com/documentation/2.1.0/JavaJsonRequests</a></p> <p>I modified my code to find a problem:</p> <pre><code>@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"); } } </code></pre> <p>The response on localhost is the json string I sent. The response on Heroku is catched exception with the String PROBLEM. It's a very weird problem, I don't know why it works ok on localhost, but not on heroku... Any ideas? :)</p> <p>UPDATE (Android logcat):</p> <pre><code>02-08 00:01:09.140: I/SendResultService(18709): &lt;!DOCTYPE html&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;html&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;head&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;title&gt;Error&lt;/title&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;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"&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;style&gt; 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): &lt;/style&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;/head&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;body&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;h1&gt;Oops, an error occured&lt;/h1&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;p id="detail"&gt; 02-08 00:01:09.140: I/SendResultService(18709): This exception has been logged with id &lt;strong&gt;6d9a6k11h&lt;/strong&gt;. 02-08 00:01:09.140: I/SendResultService(18709): &lt;/p&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;/body&gt; 02-08 00:01:09.140: I/SendResultService(18709): &lt;/html&gt; </code></pre> <p>SOLUTION: Ok, I solved a problem - it was in fact a problem in my Android code:</p> <pre><code>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")); // &lt;-- 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");} } </code></pre> <p>This code now works, I commented the problematic part: setting a BasicHeader to a string entity. This apparently wasn't problem for localhost, but it was for Heroku.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload