首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android登录认证失败

Android登录认证失败
EN

Stack Overflow用户
提问于 2015-04-07 10:01:26
回答 1查看 113关注 0票数 0

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

这是我的活动文件

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
     <?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; 
        }
         ?>
EN

回答 1

Stack Overflow用户

发布于 2015-04-07 13:01:23

如果您想使用您的php脚本而不是json格式,可以参考以下问题

Android: AsyncTask to make an HTTP GET Request?

Make an HTTP request with android

来处理响应。建议使用AsyncTask。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29482505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档