您好,我是 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/