Android登录验证失败

时间:2015-04-07 02:01:26

标签: php android mysql apache

我在xampp服务器上有我的数据库,我的活动和php文件如下所示。此页面中的问题是,当我运行应用程序时,它总是抛出异常。它总是显示我的最后一条消息,即“存在一些问题”。我的提交日期即将到期。

这是我的活动档案

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文件:

     <?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; 
        }
         ?>

1 个答案:

答案 0 :(得分:0)

如果你想用你的PHP脚本代替json格式,可以参考这些问题
Android: AsyncTask to make an HTTP GET Request?
Make an HTTP request with android以处理回复。建议使用AsyncTask。