Android Login with PHP MySQL

Login with PHP MySQL in Android – php mysql

CLASS FILE

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText UsernameEt, PasswordEt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsernameEt = (EditText) findViewById(R.id.etUsername);
PasswordEt = (EditText) findViewById(R.id.etPassord);
}

public void onLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = “login”;

BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);

}
}

XML FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:inputType=”textPersonName”
android:ems=”10″
android:id=”@+id/etUsername”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”55dp”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true”
android:hint=”UserName here” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:inputType=”textPassword”
android:ems=”10″
android:id=”@+id/etPassord”
android:layout_below=”@+id/etUsername”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignRight=”@+id/etUsername”
android:layout_alignEnd=”@+id/etUsername”
android:hint=”Password here” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Login”
android:id=”@+id/btnLogin”
android:layout_below=”@+id/etPassord”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”115dp”
android:onClick=”onLogin”
/>
</RelativeLayout>

 

BackgroundWorker Class

package com.example.malik.test;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;

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;

/**
* Created by malik on 08/06/2016.
*/
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://10.0.2.2/login.php”;
String login_url = “http://192.168.1.100/login.php”;
if(type.equals(“login”)){
try {
String user_name = 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(“user_name”, “UTF-8”)+”=”+URLEncoder.encode(user_name, “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);
}
}

Manifest FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.malik.test”>
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

</application>

</manifest>

 

CONN.PHP

<?php

$db_name = “malik”;
$mysql_username = “root”;
$mysql_password = “”;
$server_name = “localhost”;

$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);

?>

 

Login.php

<?php

require “conn.php”;
$user_name = $_POST[“user_name”];
$user_pass = $_POST[“password”];

$mysql_qry = “select * from malik_himani where username like ‘$user_name’ and password like ‘$user_pass’;”;

$result = mysqli_query($conn, $mysql_qry);

if(mysqli_num_rows($result) > 0){
echo “Login success”;
} else {
echo “Login not success”;
}

?>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.