title: Android:Username and password login using httppost
body: <p>I'm using eclipse and have been trying for a while to do login using http request and php script to connect to the server side.</p>
  
  <p>The problem is when i click the login button nothing happens,my guess is there is a problem with the OnClikListener or the data for the textfield is not been send to the server </p>
  
  <p>Here is my code.</p>
  
  <pre><code>public class LogInActivity extends Activity implements OnClickListener
  {
  
  Button ok,back,exit;
  TextView result;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  
      ok = (Button)findViewById(R.id.btn_login);
  
      ok.setOnClickListener(LogInActivity.this);
  
      result = (TextView)findViewById(R.id.lbl_result);
  
  }
  
  public void postLoginData() {
  
      HttpClient httpclient = new DefaultHttpClient();
  
  
      HttpPost httppost = new HttpPost("http://10.0.2.2/androidRegistration/login.php");
  
      try {
  
          EditText uname = (EditText)findViewById(R.id.txt_username);
          String username = uname.getText().toString();
  
          EditText pword = (EditText)findViewById(R.id.txt_password);
          String password = pword.getText().toString();
  
          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
          nameValuePairs.add(new BasicNameValuePair("username", username));
          nameValuePairs.add(new BasicNameValuePair("password", password));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
          Log.w("LogInActivity", "Execute HTTP Post Request");
          HttpResponse response = httpclient.execute(httppost);
  
          String str = inputStreamToString(response.getEntity().getContent()).toString();
          Log.w("LogInActivity", str);
  
          if(str.toString().equalsIgnoreCase("true"))
          {
              Log.w("LogInActivity", "TRUE");
              result.setText("Login successful");   
          }else
          {
              Log.w("LogInActivity", "FALSE");
              result.setText(str);                
          }
  
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  } 
  
  private StringBuilder inputStreamToString(InputStream is) {
      String line = "";
      StringBuilder total = new StringBuilder();
  
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  
      try {
          while ((line = rd.readLine()) != null) { 
              total.append(line); 
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return total;
  }
  
  @Override
  public void onClick(View view) {
      if(view == ok){
          postLoginData();
      }
  }     
  
  }
  </code></pre>