title: android with connection with php mysql using json
body: <p>i have create a login activity on android system that need connection with php and mysql and it work perfectly but now i need to transform my to get JSON  RESPONSE  but i do not know how to change my code in php and in java to make my app be applicable to the json </p>
  
  <p>if anyone can help me i will appreciate that 
   this is my code</p>
  
  <h1>check.php</h1>
  
  <pre><code>&lt;?php
  $hostname_localhost ="localhost";
  $database_localhost ="fil";
  $username_localhost =********";
  $password_localhost ="*******";
  $localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
  or
  trigger_error(mysql_error(),E_USER_ERROR);
  mysql_select_db($database_localhost, $localhost);
  $username = $_POST['username'];
  $password = $_POST['password'];
  $query_search = "select * from members where username = '".$username."' AND password = '".$password. "'";
  $query_exec = mysql_query($query_search) or die(mysql_error());
  $rows = mysql_num_rows($query_exec);
  //echo $rows;
  if($rows == 0) { 
  echo "No Such User Found"; 
  }
  else  {
  echo "User Found"; 
  }
  ?&gt;
  </code></pre>
  
  <h1>AndroidPHPConnectionDemo.java</h1>
  
  <pre><code>public class AndroidPHPConnectionDemo extends Activity {
      Button b;
      EditText et,pass;
      TextView tv;
      HttpPost httppost;
      StringBuffer buffer;
      HttpResponse response;
      HttpClient httpclient;
      List&lt;NameValuePair&gt; nameValuePairs;
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          b = (Button)findViewById(R.id.Button01);  
          et = (EditText)findViewById(R.id.username);
          pass= (EditText)findViewById(R.id.password);
          tv = (TextView)findViewById(R.id.tv);
  
  
          b.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
              login();         
              }
          });
      }
  
      void login(){
          try{            
  
              httpclient=new DefaultHttpClient();
              httppost= new HttpPost("http://10.0.2.2/check.php"); // make sure the url is correct.
              //add your data
              nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
              // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
              nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
              nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              //Execute HTTP Post Request
              response=httpclient.execute(httppost);
              // edited by James from coderzheaven.. from here....
              ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
              final String response = httpclient.execute(httppost, responseHandler);
              System.out.println("Response : " + response); 
              runOnUiThread(new Runnable() {
                  public void run() {
                      tv.setText("Response from PHP : " + response);
  
                  }
              });
  
              if(response.equalsIgnoreCase("User Found")){
                  runOnUiThread(new Runnable() {
                      public void run() {
                          Toast.makeText(AndroidPHPConnectionDemo.this,"Login Success", Toast.LENGTH_SHORT).show();
                          TextView tv2 = (TextView)findViewById(R.id.tv2);
                          tv2.setText("hello");
                      }
                  });
  
                  startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
              }else{
                  showAlert();                
              }
  
          }catch(Exception e){
  
              System.out.println("Exception : " + e.getMessage());
          }
      }
  
  
  
  
      public void showAlert(){
          AndroidPHPConnectionDemo.this.runOnUiThread(new Runnable() {
              public void run() {
                  AlertDialog.Builder builder = new AlertDialog.Builder(AndroidPHPConnectionDemo.this);
                  builder.setTitle("Login Error.");
                  builder.setMessage("User not Found.")  
                         .setCancelable(false)
                         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int id) {
                             }
                         });                     
                  AlertDialog alert = builder.create();
                  alert.show();               
              }
          });
      }
  }
  </code></pre>
  
  <h1>UserPage.java</h1>
  
  <pre><code>package pack.coderzheaven;
  
  import android.app.Activity;
  import android.os.Bundle;
  
  public class UserPage extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.userpage);
  
      }
  }
  </code></pre>