我正在开发一个Android应用程序。在我的代码的特定部分中,即使满足语句的if语句,其他选项始终执行。任何帮助将受到高度赞赏。这是我的代码:
php:
<?php
error_reporting(0);
include 'conexao.php';
$usuario = $_POST["username"];
$senha = $_POST["password"];
$result = mysql_query("select * from login where usuario = '" . $usuario . "' and senha = '" . $senha . "';");
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
echo 'false';
} else if ($num_rows == 1) {
echo 'true';
} else {
echo 'nenhum';
}
?>
和我的java:
EditText txtusuario, txtsenha;
Button btnlogin;
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtusuario = (EditText) findViewById(R.id.txtusuario);
txtsenha = (EditText) findViewById(R.id.txtsenha);
btnlogin = (Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get text from email and passord field
final String username = txtusuario.getText().toString();
final String password = txtsenha.getText().toString();
// Initialize AsyncLogin() class with email and password
new AsyncLogin().execute(username,password);
}
});
}
private class AsyncLogin extends AsyncTask<String, String, String>
{
ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://rhynotcc.hol.es/teste/login.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username", params[0])
.appendQueryParameter("password", params[1]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception";
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return(result.toString());
} else {
return("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
String teste = "true";
if(result.equals(teste)) {
//Notificação para saber o resultado do login
int a = 0;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(LoginActivity.this);
mBuilder.setSmallIcon(R.drawable.escavadeira);
mBuilder.setContentTitle("Notificação");
mBuilder.setContentText("Conexão deu certo caralho" + result);
NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNot.notify(a, mBuilder.build());
} else {
int a = 0;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(LoginActivity.this);
mBuilder.setSmallIcon(R.drawable.escavadeira);
mBuilder.setContentTitle("Notificação");
mBuilder.setContentText("Deu errado" + result);
NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNot.notify(a, mBuilder.build());
}
}
}
当我的通知中的结果是" deu errado true",当时应该是if语句,我已经尝试了 result.equals("true")
if ($num_rows == 0) {
echo 'false';
} else if ($num_rows == 1) {
echo 'true';
} else {
echo 'nenhum';
}
而不是确保您的代码应该像
一样if ($num_rows) {
echo 'false';
} else {
echo 'nenhum';
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//this method will be running on UI thread
pdLoading.dismiss();
String teste = "true";
int a = 0;
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(LoginActivity.this);
mBuilder.setSmallIcon(R.drawable.escavadeira);
mBuilder.setContentTitle("Notificação");
if (result.equals(teste)) {
//Notificação para saber o resultado do login
mBuilder.setContentText("Conexão deu certo caralho" + result);
} else {
mBuilder.setContentText("Deu errado" + result);
}
NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNot.notify(a, mBuilder.build());
}