即使结果与if语句匹配,Android也会转到'else'

时间:2017-05-03 04:54:02

标签: android mobile

首先抱歉可能出现英语错误,我的英语不太好。

现在,我的应用程序继续使用if else上的else选项,即使匹配语句时,请查看我的代码。

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")

2 个答案:

答案 0 :(得分:0)

@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());
}

答案 1 :(得分:0)

if ($num_rows == 0) {
    echo 'false';
} else if ($num_rows == 1) {
    echo 'true';
} else {
    echo 'nenhum';
}

请确保您的代码像这样

if ($num_rows) {
    echo 'false';
} else {
    echo 'nenhum';
}