虽然条件符合,但布尔值没有评估为真
所以我有一个名为isSuccessful的静态变量,所有变量都应该这样做,如果某人能够成功登录,则为true,否则为false。我已经设置为默认为false。我写的php脚本发送消息“loginsuccess”并将其存储在onProgressUpdate参数中。我调试,看看这是什么被存储在参数中,编译说它是。那么我无法弄清楚为什么isSuccessful没有被切换为真。我决定这样做。一旦发生这种情况,我将登录活动称为homeScreen活动。 LoginTask:虽然条件符合,但布尔值没有评估为真
public class LogInTask extends AsyncTask<String, String,String> {
public Scanner reader;
Formatter writer;
Context mcontext;
//if Login was successful
public static boolean isSuccessful;
LogInTask(Context context)
{
mcontext = context;
}
URL url;
URLConnection con;
String output = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
isSuccessful=false;
try {
url = new URL("http://192.168.1.75:1234/login.php");
con = url.openConnection();
//allows to send information
con.setDoOutput(true);
//allows to receive information
con.setDoInput(true);
writer = new Formatter(con.getOutputStream());
//Sends login information to SQL table
writer.format("user_name="+params[0]+"&password="+params[1]);
writer.close();
//Reads input
reader = new Scanner(con.getInputStream());
while(reader.hasNext())
{
output+= reader.next();
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
publishProgress(output);
return output;
}
@Override
protected void onProgressUpdate(String... values) {
Toast.makeText(mcontext, values[0],Toast.LENGTH_LONG).show();
if(values[0]=="loginsuccess")
isSuccessful = true;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
LogInActivity:
public class LogInActivity extends Activity {
private Typeface fontRobo;
private TextView logoText;
private EditText userName;
private EditText passWord;
private TextView dontHave;
private TextView signUp;
private Button logIn;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
i = new Intent(this, HomeActivity.class);
//Logo
logoText = (TextView)findViewById(R.id.Logo);
fontRobo = Typeface.createFromAsset(this.getAssets(),"fonts/ROBO.ttf");
logoText.setText("ArtSpace");
logoText.setTypeface(fontRobo);
//Don't have an account?
dontHave = (TextView) findViewById(R.id.Donthave);
dontHave.setTypeface(fontRobo);
//Sign Up
signUp = (TextView) findViewById(R.id.signUP);
signUp.setTypeface(fontRobo);
userName = (EditText) findViewById(R.id.userName);
passWord = (EditText) findViewById(R.id.passWord);
logIn = (Button) findViewById(R.id.LogIn);
}
//Log in button event
public void logInClick(View view)
{
final LogInTask task = new LogInTask(LogInActivity.this);
task.execute(userName.getText().toString(), passWord.getText().toString());
if(LogInTask.isSuccessful)
startActivity(i);
}
PHP:
<?php
require "conn.php";
$user_name = $_POST['user_name'];
$user_pass = $_POST['password'];
$mysql_qry = "SELECT * FROM login WHERE UserName LIKE '$user_name' AND Password LIKE '$user_pass';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) == true)
{
echo "login success";
}
else
{
echo "login not success";
}
?>
task.execute()
是的AsyncTask又名它需要时间来执行。但是在你打电话之后,你正在检查。您需要在onPostExecute()
区块中检查isSuccessful。 事情是这样的:
final LogInTask task = new LogInTask(LogInActivity.this){
@Override
protected void onPostExecute(String s) {
if(LogInTask.isSuccessful)
startActivity(i);
}};
task.execute(userName.getText().toString(), passWord.getText().toString());
PS:别的东西,不==
使用.equals()
if(values[0].equals("loginsuccess"))
尝试过,仍然没有运气。再次调试它,s等于“loginsuccess”,但仍然由于某种原因,或者其他isSuccessful仍然是错误的。 – user2626734
是的,这是我的一个愚蠢的错误。你的解决方案奏效谢谢。 – user2626734
顺便说一句,当你输入像tgat这样的方法时,它叫什么名字?你把这个方法放在变量里面。那叫什么? – user2626734
比较字符串没有PHP这里 – nogad
@nogad加入PHP – user2626734
你的SQL查询1)开放sql注入,2)使用LIKE代替精确比较,3)让数据库进行字符串比较,可能不区分大小写,4)使用以明文形式存储的密码。 – sisve