0

My app crashes at "Fail 3".

This is the php file for basic connection string.

<?php 
$db_name = "id2164700_scheduler";
$mysql_username = "id2164700_admin";
$mysql_password = "admin";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, 
$mysql_password,$db_name);
?>

This code is for the login.php that will be used below in the activity.

 <?php 
require "config.php";

$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];

 $mysql_qry = "select * from users where username='$user_name' and 
 password='$user_pass';";
 $result = mysqli_query($conn ,$mysql_qry);

 if(mysqli_num_rows($result) > 0) {
 $flag=array("code"=>TRUE);
  } 

 else {
 $flag=array("code"=>FALSE);
  }

  json_encode($flag);

  ?>

This is the android code. This is the code for when the Login button is clicked.

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            uname = username.getText().toString();
            pword = password.getText().toString();

          String type = "login";
          /*  SQLiteConnect1 backgroundWorker = new SQLiteConnect1(getApplicationContext());
            backgroundWorker.execute(type, uname, pword); */



           /* if (db.validation(uname, pword)){
                Toast.makeText(getApplicationContext(), "Hurray!!", Toast.LENGTH_LONG).show();
                Intent mynew = new Intent(getApplicationContext(),calenderView.class);
                startActivity(mynew);
            }else{
                Toast.makeText(getApplicationContext(), "Did not work!!", Toast.LENGTH_LONG).show();
            }*/


            final class Des extends AsyncTask<Void, Void, Void> {

                @Override
                protected void onPreExecute() {


                    super.onPreExecute();

                    mProgressDialog = new ProgressDialog(MainActivity.this);
                    mProgressDialog.setMessage("Verifying your account..A moment please");
                    mProgressDialog.setIndeterminate(false);
                    mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    mProgressDialog.setCancelable(true);
                    mProgressDialog.show();

                    Uri.Builder builder = new Uri.Builder()
                            .appendQueryParameter("user_name", uname.trim())
                     .appendQueryParameter("password", pword.trim());
                    query = builder.build().getEncodedQuery();


                }

                @Override
                protected Void doInBackground(Void... params) {


                    InputStream is = null;
                    try {

                        String url = "http://canny-intensities.000webhostapp.com/login.php";
                        URL obj = new URL(url);
                        con = (HttpURLConnection) obj.openConnection();
                        con.setRequestMethod("POST");
                        con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
                        con.setRequestProperty("Accept-Language", "UTF-8");
                        con.setDoOutput(true);
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(con.getOutputStream());
                        outputStreamWriter.write(query);
                        outputStreamWriter.flush();
                        Log.e("pass 1", "connection success ");
                    } catch (Exception e) {
                        Log.e("Fail 1", e.toString());

                    }


                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        String line;
                        StringBuffer sb = new StringBuffer();

                        while ((line = in.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        //   is.close();
                        resulta = sb.toString();
                        Log.e("pass 2", "connection success ");
                    } catch (Exception e) {
                        Log.e("Fail 2", e.toString());
                    }
                    return null;
                }


                @Override
                protected void onPostExecute(Void result) {


                    try {
                        json_data = new JSONObject(resulta);
                        int code = (json_data.getInt("code"));
                        if (code == 1) {

                            final android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(MainActivity.this);
                            LinearLayout lila1 = new LinearLayout(MainActivity.this);
                            lila1.setOrientation(LinearLayout.VERTICAL);
                            alert.setView(lila1);
                            alert.setTitle("Success");

                            alert.setMessage("Successful login");
                           // alert.setIcon(R.drawable.succ);
                            alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            });
                            alert.show();
                        }  else {
                            final android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(MainActivity.this);
                            LinearLayout lila1 = new LinearLayout(MainActivity.this);
                            lila1.setOrientation(LinearLayout.VERTICAL);
                            alert.setView(lila1);
                            alert.setTitle("Failed!");
                            //alert.setMessage((json_data.getString("error_info")));
                           // alert.setIcon(R.drawable.e);
                            Log.e("Fail 3", "Value " + code);
                            alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }


                            });
                            alert.show();

                        }
                    } catch (Exception e) {
                        Log.e("Fail 3", e.toString());

                    }

                    mProgressDialog.dismiss();


                }





            }//end of else
             new Des().execute();


        }
    });

}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Hrshah
  • 1
  • 3
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 12 '17 at 16:58
  • 3
    **Never store plain text passwords!** Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jul 12 '17 at 16:59
  • In your Java code, you have two instances of "Fail 3" logs. This will make it harder to work out where the problem is. Can you fix that first, and update the question? – halfer Jul 12 '17 at 18:25

0 Answers0