title: Send a JSON Object from Android to PHP server with POST method and HttpURLConnection
body: <p>I'm trying to establish communication between my Android app and my WampServer on local network.</p>
  
  <p>When I want to read data from the server, I have had success, but I have a problem when I try to send data to the server.</p>
  
  <p>I'm using an Service to established the communication :</p>
  
  <pre><code>public class SynchronisationService extends Service {
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId);
  
      new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  URL url = new URL("http://192.168.37.23/happiness_barometer/php_input.php");
                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                  connection.setDoOutput(true);
                  connection.setDoInput(false);
                  connection.setRequestMethod("POST");
                  connection.connect();
                  OutputStream outputStream = connection.getOutputStream();
                  OutputStreamWriter writer = new OutputStreamWriter(outputStream);
                  JSONObject jsonObject = new JSONObject();
                  jsonObject.put("rate", 1);
                  writer.write(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
                  writer.flush();
                  writer.close();
  
              } catch (Exception e) {
                  Log.v("EXCEPTION", e.getMessage());
              }
          }
      }).start();
  
  
      stopSelf();
  
      return flags;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And my php file:</p>
  
  <pre><code> &lt;?php 
  
      try 
      {
          $bdd = new PDO('mysql:host=localhost;dbname=happiness_barometer;charset=utf8', 'utilisateur', '');
      } catch (Exception $e) 
      {
          die('Erreur : '.$e-&gt;getMessage());
      }
  
      $sql = $bdd-&gt;prepare(
      'INSERT INTO rates (rate, comment, category, day, month, year, hour, minute, day_of_week, week, rate_number) 
      VALUES (:rate, :comment, :category, :day, :month, :year, :hour, :minute, :day_of_week, :week, :rate_number)');
      if (!empty($_POST['rate'])) {
          $sql-&gt;execute(array(
              'rate' =&gt; $_POST['rate'],
              'comment' =&gt; '',
              'category' =&gt; 'pro',
              'day' =&gt; 19,
              'month' =&gt; 8,
              'year' =&gt; 2015,
              'hour' =&gt; 18,
              'minute' =&gt; 3,
              'day_of_week' =&gt;3,
              'week' =&gt; 33,
              'rate_number' =&gt; 2));
      }
  ?&gt;
  </code></pre>
  
  <p>When I run my app, nothing is added to my database.
  I think there is nothing in the <code>$_POST['rate']</code>.</p>
  
  <p>Please, tell me what is wrong in my code?</p>