title: Http Post on Android: Server Request and Response
body: <p>I am trying to send information to a server from an android phone. </p>
  
  <p>When the server receives the information it sends back a 1 or a 0 to signify pass or fail. Server-side everything is fine because there in another app for iOS that does the same thing, but it works. Also the sever sends an email anytime it receives a request. </p>
  
  <p>My problem is that it does not seem like the app is contacting the server. When I run the app no response is given back and no email is sent once the Http Post code is executed.</p>
  
  <p>I have the Http Post code below, thank you for any help you can give .</p>
  
  <pre><code>    public void send()
          {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(site);
  
              try {
                   // Add your data
                  List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
                  nameValuePairs.add(new BasicNameValuePair("dateOfEventStart", startYear + "-" + startMonth + "-" + startDay));
                  nameValuePairs.add(new BasicNameValuePair("dateOfEventEnd", endYear + "-" + endMonth + "-" + endDay));
                  nameValuePairs.add(new BasicNameValuePair("locationType", locType));
                  nameValuePairs.add(new BasicNameValuePair("locationZipCode", location));
                  nameValuePairs.add(new BasicNameValuePair("eventType", type));
                  nameValuePairs.add(new BasicNameValuePair("groundSurface", groundType));
                  nameValuePairs.add(new BasicNameValuePair("numberOfGuests", numGuests + ""));
                  nameValuePairs.add(new BasicNameValuePair("seatingArrangments", arrangement));
                  nameValuePairs.add(new BasicNameValuePair("accessoriesTables",stuff));
                  nameValuePairs.add(new BasicNameValuePair("estimatedArea", tent));
                  nameValuePairs.add(new BasicNameValuePair("estimatedRoomToSpare", spared));
                  nameValuePairs.add(new BasicNameValuePair("contactName", nameA));
                  nameValuePairs.add(new BasicNameValuePair("contactPhone", phoneA));
                  nameValuePairs.add(new BasicNameValuePair("contactEmail", addressA));
                  nameValuePairs.add(new BasicNameValuePair("contactComments", comment));
                  nameValuePairs.add(new BasicNameValuePair("isInternational", isInternational + ""));
                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
                  // Execute HTTP Post Request
                  HttpResponse response = httpclient.execute(httppost);
                  responseString = response.toString();
  
  
              } catch (ClientProtocolException e) {
                  // TODO Auto-generated catch block
              } catch (IOException e) {
                  // TODO Auto-generated catch block
              }
  
          }
  </code></pre>
  
  <p>site is variable that was declared earlier on. It is a string that contains location of the form receiver </p>
  
  <p>also below  is the information I was given about the server </p>
  
  <blockquote>
    <p>You will send it a “POST” request with (most likely) a “content-type” of “application/x-www-form-urlencoded”.</p>
    
    <p>The content of the “POST” will be a String that is formatted like a website URL’s “Query >String”. (That’s the stuff after the question mark if you’re not familiar with them.)</p>
    
    <p>In this string, there are keys and values connected by an equal sign. Key/Value pairs are >separated by ampersands (&amp;’s).</p>
    
    <p>Here is an example of a string I used in ASP to test the service if it helps (with >carriage returns added for readability):</p>
    
    <p>strContent = "dateOfEventStart=2011-09-24</p>
    
    <p>&amp;dateOfEventEnd=2011-09-26</p>
    
    <p>&amp;locationType=" &amp; Server.URLEncode("Residential") &amp; "</p>
    
    <p>&amp;locationZipCode=38016</p>
    
    <p>&amp;eventType=" &amp; Server.URLEncode("Corporate Event") &amp; "</p>
    
    <p>&amp;eventTypeSecondary=</p>
    
    <p>&amp;groundSurface=" &amp; Server.URLEncode("Gravel (over dirt)") &amp; "</p>
    
    <p>&amp;groundSurfaceSecondary=</p>
    
    <p>&amp;numberOfGuests=90</p>
    
    <p>&amp;seatingArrangements=" &amp; Server.URLEncode("Round tables of 6 guests") &amp; "</p>
    
    <p>&amp;accessoriesTables=" &amp; Server.URLEncode("Buffet,Cake,Gift,Beverage Station") &amp; "</p>
    
    <p>&amp;accessoriesEntertainment=</p>
    
    <p>&amp;estimatedArea=" &amp; Server.URLEncode("1200 sq ft") &amp; "</p>
    
    <p>&amp;estimatedTentSize=" &amp; Server.URLEncode("30 ft x 40 ft or 20 ft x 60 ft") &amp; "</p>
    
    <p>&amp;estimatedRoomToSpare=" &amp; Server.URLEncode("0 sq ft or 0 sq ft") &amp; "</p>
    
    <p>&amp;contactName=" &amp; Server.URLEncode("Jonathan Chan") &amp; "</p>
    
    <p>&amp;contactPhone=9011234567</p>
    
    <p>&amp;contactEmail=" &amp; Server.URLEncode("jchan@lunaweb.com") &amp; "</p>
    
    <p>&amp;contactComments=" &amp; Server.URLEncode("This is a long comment.") </p>
    
    <p>In my ASP example, you may have noticed “Server.URLEncode” surrounding Strings. This is >so certain characters that could mess up the data gets encoded into % hexadecimal ASCII >values. For example, if someone enters “I love cheese &amp; cake” as his comment, the program >will think that the ampersand is denoting a new Key/Value pair. If we encode the URL, it >will look like “I%20love%20cheese%20%26%20cake”.</p>
  </blockquote>