Saidah Morales
Lurker
I need to connect to my database in phpmyadmin somehow, I've followed through this video
And even tried changing some things but when i try out my app so far on my phone, I just get the login status title. The php code works fine on my laptop itself. Can't try it on a Virtual Device since it's not compatible for my laptop, so I run it on my phone.
Here are the codes I have:
loginnn.php:
smth.php:
And then for Android Studio
AndroidManifest.xml:
Login.java:
and BackgroundWorker.java:
I've tried what I can, and I have this defense in two days or so and It would really help if anyone can figure this out, maybe there's something i haven't noticed, a useful line of code, anything!
And even tried changing some things but when i try out my app so far on my phone, I just get the login status title. The php code works fine on my laptop itself. Can't try it on a Virtual Device since it's not compatible for my laptop, so I run it on my phone.
Here are the codes I have:
loginnn.php:
PHP:
<?php
require "smth.php";
$user_name = $_POST["username"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from UserLogin where Email = '$user_name' and Pass = '$user_pass';";
$result = mysqli_query($conn , $mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "Login Success";
}
else{
echo "Login Not Success";
}
?>
smth.php:
PHP:
<?php
$db_name = "sailyx8096";
$mysql_username = "Sailyx8096";
$mysql_password = "thisiscorrectforsureanyway";
$server_name= "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
?>
And then for Android Studio
AndroidManifest.xml:
Java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="trogi.herbalifepro.POS">
<uses-permission android:name="android.permission.INTERNET"/>
<application android:usesCleartextTraffic="true">
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Menu" />
<activity android:name=".Registration"></activity>
</application>
</manifest>
Login.java:
Java:
package trogi.herbalifepro.POS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Login extends Activity {
private EditText User;
private EditText Pass;
public Button Login;
public Button Register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
User = findViewById(R.id.ETUser);
Pass = findViewById(R.id.ETPass);
Login = findViewById(R.id.BTNLogin);
Register = findViewById(R.id.BTNSIGNUP);
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Login.this, Registration.class);
startActivity(intent);
}
});
}
public void OnLogin(View view){
String username = User.getText().toString();
String password = Pass.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}
}
and BackgroundWorker.java:
Java:
package trogi.herbalifepro.POS;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.0.19/loginnn.php";
if (type.equals("login")) {
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username", "UTF-8" )+"=" + URLEncoder.encode(username, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8" )+ "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream ();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
I've tried what I can, and I have this defense in two days or so and It would really help if anyone can figure this out, maybe there's something i haven't noticed, a useful line of code, anything!