title: Sending String from Android to PHP Server via POST
body: <p>In an Android app I am creating I am attempting to send a username in the form of a string to my PHP web server. My code compiles without error, and I know the variable I'm passing to the function isn't null (toasted the variable and the value is what I want). However, when I try to get the string by using a POST request, the string I want is null. How can I retrieve the string?</p>
  
  <p>On A side note, I am new to Android and I'm not sure as to weather or not I should be putting this in a class that extends AsyncTask. I had to do that in another function that I used to retrieve data from the web server. Here is my code:</p>
  
  <pre><code>public void sendInfo(String url, String user, String pass){
      // Create a new HttpClient and Post Header
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(url);
  
      try{
          // Add the data
          List&lt;NameValuePair&gt; pairs = new ArrayList&lt;NameValuePair&gt;(2);
          pairs.add(new BasicNameValuePair("username", user));
          pairs.add(new BasicNameValuePair("password", pass));
          post.setEntity(new UrlEncodedFormEntity(pairs));
  
          // Execute HTTP Post request
          HttpResponse response = client.execute(post);
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e){
          e.printStackTrace();
      } catch (IOException e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And the PHP code I am using to retrieve the post:</p>
  
  <pre><code>// ...... Code to connect to database works fine
  
  $new_username = $_POST['username'];
  
  if(is_null($new_username)){
      echo 'string is null';
  }
  
  echo $new_username;
  </code></pre>
  
  <p>When I view the web page, I only see the text "string is null"</p>