title: HttpURLConnection sending JSON POST request to Apache/PHP
body: <p>I'm struggling with HttpURLConnection and OutputStreamWriter.</p>
  
  <p>The code actually reaches the server, as I do get a valid error
  response back. A POST request is made, but no data is received
  server-side.</p>
  
  <p>Any hints to proper usage of this thingy is highly appreciated.</p>
  
  <p>The code is in an AsyncTask</p>
  
  <pre><code>protected JSONObject doInBackground(Void... params) {                                   
      try {                                                                               
          url = new URL(destination);                                                     
          client = (HttpURLConnection) url.openConnection();                              
          client.setDoOutput(true);                                                       
          client.setDoInput(true);                                                        
          client.setRequestProperty("Content-Type", "application/json; charset=UTF-8");   
          client.setRequestMethod("POST");                                                
          //client.setFixedLengthStreamingMode(request.toString().getBytes("UTF-8").length);
          client.connect();                                                               
  
          Log.d("doInBackground(Request)", request.toString());                           
  
          OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());   
          String output = request.toString();                                             
          writer.write(output);                                                           
          writer.flush();                                                                 
          writer.close();                                                                 
  
          InputStream input = client.getInputStream();                                    
          BufferedReader reader = new BufferedReader(new InputStreamReader(input));       
          StringBuilder result = new StringBuilder();                                     
          String line;                                                                    
  
          while ((line = reader.readLine()) != null) {                                    
              result.append(line);                                                        
          }                                                                               
          Log.d("doInBackground(Resp)", result.toString());                               
          response = new JSONObject(result.toString());                                   
      } catch (JSONException e){                                                          
          this.e = e;                                                                     
      } catch (IOException e) {                                                           
          this.e = e;                                                                     
      } finally {                                                                         
          client.disconnect();                                                            
      }                                                                                   
  
      return response;                                                                    
  }                                                                                       
  </code></pre>
  
  <p>The JSON I'm trying to send:</p>
  
  <pre><code>JSONObject request = {
      "action":"login",
      "user":"mogens",
      "auth":"b96f704fbe702f5b11a31524bfe5f136efea8bf7",
      "location":{
          "accuracy":25,
          "provider":"network",
          "longitude":120.254944,
          "latitude":14.847808
          }
      };
  </code></pre>
  
  <p>And the response I get from the server:</p>
  
  <pre><code>JSONObject response = {
      "success":false,
      "response":"Unknown or Missing action.",
      "request":null
      };
  </code></pre>
  
  <p>And the response I should have had:</p>
  
  <pre><code>JSONObject response = {
      "success":true,
      "response":"Welcome Mogens Burapa",
      "request":"login"
      };
  </code></pre>
  
  <p>The server-side PHP script:</p>
  
  <pre><code>&lt;?php
  
      $json = file_get_contents('php://input');
      $request = json_decode($json, true);
  
      error_log("JSON: $json");
  
      error_log('DEBUG request.php: ' . implode(', ',$request));
      error_log("============ JSON Array ===============");
      foreach ($request as $key =&gt; $val) {
          error_log("$key =&gt; $val");
      }
  
      switch($request['action'])
      {
          case "register":
  
              break;
          case "login":
              $response = array(
                              'success' =&gt; true,
                              'message' =&gt; 'Welcome ' . $request['user'],
                              'request' =&gt; $request['action']
                          );
              break;
          case "location":
  
              break;
          case "nearby":
  
              break;
          default:
              $response = array(
                              'success' =&gt; false,
                              'response' =&gt; 'Unknown or Missing action.',
                              'request' =&gt; $request['action']
                          );
              break;
      }
  
      echo json_encode($response);
  
      exit;
  
  
  ?&gt;
  </code></pre>
  
  <p>And the logcat output in Android Studio:</p>
  
  <pre><code>D/doInBackground(Request)﹕ {"action":"login","location":{"accuracy":25,"provider":"network","longitude":120.254944,"latitude":14.847808},"user":"mogens","auth":"b96f704fbe702f5b11a31524bfe5f136efea8bf7"}
  D/doInBackground(Resp)﹕ {"success":false,"response":"Unknown or Missing action.","request":null}
  </code></pre>
  
  <p>If I append <code>?action=login</code> to the <code>URL</code> I can get a success response from the server. But only the <strong>action</strong> parameter registers server-side.</p>
  
  <p><code>{"success":true,"message":"Welcome ","request":"login"}</code></p>
  
  <p>The conclusion must be that no data is transferred by <code>URLConnection.write(output.getBytes("UTF-8"));</code> </p>
  
  <p>Well, data get transferred after all.</p>
  
  <p>Solution offered by @greenaps does the trick:</p>
  
  <pre><code>$json = file_get_contents('php://input');
  $request = json_decode($json, true);
  </code></pre>
  
  <p>PHP script above updated to show the solution.</p>