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