title: sending JSON to server using POST in android app
body: <p>I'm creating an android app to send JSON to a server-side php script and store it in a database</p>
  
  <p>Here's the relevant code from the app</p>
  
  <pre><code>public String POST(){
      String url = "http://192.168.150.1/t2.php";
      InputStream inputStream = null;
      String result = "";
      try {
  
          HttpClient httpclient = new DefaultHttpClient();
  
          HttpPost httpPost = new HttpPost(url);
  
          String json = "";
  
          JSONObject jsonObject = new JSONObject();
          jsonObject.put("str", "bang");
          jsonObject.put("lat", 3.10);
          jsonObject.put("lon", 3.10);
  
          json = jsonObject.toString();
          Toast.makeText(this, json, Toast.LENGTH_LONG).show();
  
          StringEntity se = new StringEntity(json);
  
  
          httpPost.setEntity(se);
  
  
          httpPost.setHeader("Accept", "application/json");
          httpPost.setHeader("Content-type", "application/json");
  
  
          HttpResponse httpResponse = httpclient.execute(httpPost);
  
  
          inputStream = httpResponse.getEntity().getContent();
  
  
  
          if(inputStream != null)
              result = "success";
          else
              result = "Did not work!";
  
      } catch (Exception e) {
          Log.d("InputStream", e.getLocalizedMessage());
      }
  
  
      return result;
  }
  </code></pre>
  
  <p>and here's the php script</p>
  
  <pre><code> &lt;?php
  
      $connection = mysql_connect("192.168.150.1","****","****"); 
      if (!$connection) {
          die("Database connection failed: " . mysql_error());
      }
           echo "connection success\n";
          $db_select = mysql_select_db("ps1",$connection);
      if (!$db_select) {
          die("Database selection failed: " . mysql_error());
      }
      echo "db selections success";
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt; hl&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;p&gt;hello&lt;/p&gt;    
  &lt;?php
  
  
  $js = json_decode(file_get_contents('php://input'));    
   //$js = json_decode($_POST); ------------ ******
  $atr ='';
  $val = '';
  foreach ($js as $a =&gt; $v)
  {
   $atr = $atr.','.$a;
   $val = $val.','.$v;
  }
  
  
  $atr = ltrim($atr,',');
  $val = ltrim($val,',');
  
  echo "insert into js (".$atr.") values (" .$val . ");";
  $result = mysql_query("insert into js (".$atr.") values (" .$val . ");", $connection);
      if (!$result) {
          die("Database query failed: " . mysql_error());
      }       
      ?&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
  
      mysql_close($connection);
  ?&gt;
  </code></pre>
  
  <p>Now if try to send a JSON from object from the app, nothing happens</p>
  
  <p>but if I try</p>
  
  <p>curl -H "Content-Type: application/json" -d '{"str":"\"hello\"","lat":"3.1","lon":"5.2"}' localhost/t2.php</p>
  
  <p>from the command-line it works.</p>
  
  <p>Also if I try to uncomment the $js = json_decode($_POST); line in the PHP script and comment out the php://input line, then the JSON object is not transferred but the values "", 0,0 are inserted into the database</p>
  
  <p>I think there must be an error with the app code.I'm following the tutorial and code from here <a href="http://hmkcode.com/android-send-json-data-to-server/" rel="nofollow">http://hmkcode.com/android-send-json-data-to-server/</a></p>
  
  <p>QUESTIONS   </p>
  
  <ol>
  <li>Can someone explain what I'm doing wrong and a plausible solution?</li>
  <li>what's the difference between php://input and $_POST?</li>
  </ol>
  
  <p>Thanks</p>
  
  <p>QUEST</p>