0

I have My databases on an xampp server and my activity and php file is given below. The problem in this page is that when I run application it always throws an exception. It always shows my last message that there is "Some problem is there." My submission date is due soon.

This is my Activity file

public class AdminLoginActivity extends Activity {


  TextView username, password, message;
  Button SignIn;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin_login);
    StrictMode.enableDefaults();

    username = (TextView) findViewById(R.id.adminusername);
    password = (TextView) findViewById(R.id.adminpassword);
    message = (TextView) findViewById(R.id.textView5);
    SignIn = (Button) findViewById(R.id.adminloginbtn);

    SignIn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            checkAdminData();

        }
    });
}

public void checkAdminData()
{

    InputStream isr = null;
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:1234/adminlogin.php");

    try
    {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>                      (2);
        nameValuePairs.add(new BasicNameValuePair("username",                 username.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password",  password.getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();

    }
    catch (ClientProtocolException e)
    {
        Log.e("log_tag", "Error in Http connection " + e.toString());
        message.setText("couldn't connect to database");
    }
    catch (IOException e)
    {

    }

    String responseText = null;
    try
    {
        String res = isr.toString();
      //  res = res.replaceAll("\\s+", "");
        if (res.equals("1")) {
            Intent i = new Intent(this, AdminMainPage.class);
            startActivity(i);
                  }
        else{

            message.setText("Username or Password incorrect");
        }
    }
    catch (Exception e)
    {

        Log.e("log_tag", "Sorry!! Incorrect Username or Password " + e.toString());
        message.setText("Some problem is there");
     }
   }
 }

PHP file:

     <?php
      $hostname_localhost ="localhost";
      $database_localhost ="sibuilder";
      $username_localhost ="root";
      $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 login l, admin a where a.username = '".$username."' AND l.password = '".$password. "'";
      $query_exec = mysql_query($query_search) or die(mysql_error());
      $rows = mysql_num_rows($query_exec);
      //echo $rows;
      if($rows == 0) { 
        echo 0; 
       }
       else  {
       echo 1; 
        }
         ?>
tjd
  • 4,064
  • 1
  • 24
  • 34

1 Answers1

0

If you want to use your php script instead json format, can refer these questions
Android: AsyncTask to make an HTTP GET Request?
Make an HTTP request with android
to handle the response. Using AsyncTask is recommended.

Community
  • 1
  • 1
zzas11
  • 1,115
  • 9
  • 16