php - 在android中使用php登录

标签 php android mysql

您好,我是 android 的新手,我正在尝试使用 php 将 android 连接到 MySQL。我想创建一个只有管理员才能登录的登录系统。 下面是我的代码

@Override
    protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.
        Boolean s = true;
        String link = "http://localhost/eKos/login.php";

        try{
            URL url = new URL(link);
            String data  = URLEncoder.encode("username", "UTF-8") + "=" +
                    URLEncoder.encode(mUsername, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8") + "=" +
                    URLEncoder.encode(mPassword, "UTF-8");

            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            wr.write(data);
            wr.flush();

            BufferedReader reader = new BufferedReader(new
                    InputStreamReader(conn.getInputStream()));

            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null) {
                sb.append(line);
                break;
            }

            if (sb.toString() != "Login Successful"){
                s = false;
            }

            } catch (Exception e){
            System.out.print("Exception: " + e.getMessage());
        }
            return s;
    }

但是,似乎变量 s 在 try block 中根本没有被修改,因为这个函数总是返回 true。 有谁知道如何解决这一问题? 谢谢

最佳答案

试试这个:

  if (!(sb.toString()).equals("Login Successful")){
            s = false;
        }

        } catch (Exception e){
        System.out.print("Exception: " + e.getMessage());
        s = false;
    }
        return s;

同时打印您的服务器响应是否包含 Login Successful 字符串。 并检查是否有异常抛出

关于php - 在android中使用php登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517510/

相关文章:

php - 如何使用 CURL 解析 html 文件的内容?

php - 如何创建多层次的 'tree' ? (如果它可以称为一棵树的话)

android - 如何使用 Espresso 匹配此表格单元格?

android - 将 PreferenceFragment 与多个首选项文件重用

php - 在 PHP 中添加 Echo

javascript - 在 WordPress 中包含包含 Php 的 JavaScript

java - 如何将图库图像作为输入流

php - 选择前 11 个,它们共同构成 MySQL 中的最高综合得分?

包含数字的 PHP 插入语句错误

php - 我可以使用 php web 服务将 android(Eclipse) 连接到 SQL Server(2005) 吗?