title: Android JSON HttpClient to send data to PHP server with HttpResponse
body: <p>I am currently trying to send some data from and Android application to a php server (both are controlled by me). </p>
  
  <p>There is alot of data collected on a form in the app, this is written to the database. This all works.</p>
  
  <p>In my main code, firstly I create a JSONObject (I have cut it down here for this example):</p>
  
  <pre><code>JSONObject j = new JSONObject();
  j.put("engineer", "me");
  j.put("date", "today");
  j.put("fuel", "full");
  j.put("car", "mine");
  j.put("distance", "miles");
  </code></pre>
  
  <p>Next I pass the object over for sending, and receive the response:</p>
  
  <pre><code>String url = "http://www.server.com/thisfile.php";
  HttpResponse re = HTTPPoster.doPost(url, j);
  String temp = EntityUtils.toString(re.getEntity());
  if (temp.compareTo("SUCCESS")==0)
  {
      Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
  }
  </code></pre>
  
  <p>The HTTPPoster class:</p>
  
  <pre><code>public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
  {
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost request = new HttpPost(url);
      HttpEntity entity;
      StringEntity s = new StringEntity(c.toString());
      s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
      entity = s;
      request.setEntity(entity);
      HttpResponse response;
      response = httpclient.execute(request);
      return response;
  }
  </code></pre>
  
  <p>This gets a response, but the server is returning a 403 - Forbidden response.</p>
  
  <p>I have tried changing the doPost function a little (this is actually a little better, as I said I have alot to send, basically 3 of the same form with different data - so I create 3 JSONObjects, one for each form entry - the entries come from the DB instead of the static example I am using).</p>
  
  <p>Firstly I changed the call over a bit:</p>
  
  <pre><code>String url = "http://www.myserver.com/ServiceMatalan.php";
  Map&lt;String, String&gt; kvPairs = new HashMap&lt;String, String&gt;();
  kvPairs.put("vehicle", j.toString());
  // Normally I would pass two more JSONObjects.....
  HttpResponse re = HTTPPoster.doPost(url, kvPairs);
  String temp = EntityUtils.toString(re.getEntity());
  if (temp.compareTo("SUCCESS")==0)
  {
      Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
  }
  </code></pre>
  
  <p>Ok so the changes to the doPost function:</p>
  
  <pre><code>public static HttpResponse doPost(String url, Map&lt;String, String&gt; kvPairs) throws ClientProtocolException, IOException 
  {
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(url);
      if (kvPairs != null &amp;&amp; kvPairs.isEmpty() == false) 
      {
          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(kvPairs.size());
          String k, v;
          Iterator&lt;String&gt; itKeys = kvPairs.keySet().iterator();
          while (itKeys.hasNext()) 
          {
              k = itKeys.next();
              v = kvPairs.get(k);
              nameValuePairs.add(new BasicNameValuePair(k, v));
          }             
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      }
      HttpResponse response;
      response = httpclient.execute(httppost);
      return response;
  }
  </code></pre>
  
  <p>Ok So this returns a response 200</p>
  
  <pre><code>int statusCode = re.getStatusLine().getStatusCode();
  </code></pre>
  
  <p>However the data received on the server cannot be parsed to a JSON string. It is badly formatted I think (this is the first time I have used JSON):</p>
  
  <p>If in the php file I do an echo on $_POST['vehicle'] I get the following:</p>
  
  <pre><code>{\"date\":\"today\",\"engineer\":\"me\"}
  </code></pre>
  
  <p>Can anyone tell me where I am going wrong, or if there is a better way to achieve what I am trying to do? Hopefully the above makes sense!</p>