即使结果与 if 语句匹配,Android 也会“else”
Posted
技术标签:
【中文标题】即使结果与 if 语句匹配,Android 也会“else”【英文标题】:Android going to 'else' even when result matches the if statement 【发布时间】:2017-05-03 04:54:01 【问题描述】:我正在开发一个安卓应用程序。 在我的代码的特定部分,总是执行 else 选项,即使满足 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' 当 true 应该是 if 语句时,我已经尝试过 result.equals("true")
【问题讨论】:
不知道为什么人们不能帮助新的 SO 成员...放一个日志并打印结果的值。 你能举个小例子吗? 在onPostExecute中,result参数的值是多少? 我解决了,我不知道为什么,但我的回声返回“真”或“假”,开头有一个空格--'。 感谢您的帮助 【参考方案1】:if ($num_rows == 0)
echo 'false';
else if ($num_rows == 1)
echo 'true';
else
echo 'nenhum';
请确保您的代码应该是这样的
if ($num_rows)
echo 'false';
else
echo 'nenhum';
【讨论】:
【参考方案2】:@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());
【讨论】:
仍然显示'deu errado true',这是else语句:/以上是关于即使结果与 if 语句匹配,Android 也会“else”的主要内容,如果未能解决你的问题,请参考以下文章
即使有一个带有特定代码的if语句用于我的输入,我的程序也会在我输入输入时停止[关闭]
即使找到的地址不在绑定框中,android geocoder getFromLocationName 也会返回结果
即使在初始化结果参数之后,CUDA atomicAdd也会产生错误的结果
即使匹配视频大小和预览大小纵横比,MediaRecorder 也会失败