Login and Registration API on Web Services:-
In this tutorial, I am discussing web services implementation for login and registration forms in very simple steps.
I have created restful Web services using PHP and MYSQL databases and hosted them into our live server ShivaConceptSolution.com/webservices.
Step by Step implementation:-
Step1st:-
Create an Android Project using Android Studio
Step 2nd:- Create Normal java class and name it HttpHandler under src package of android.
Code for Handler
package com.example.testapp;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public String makeServiceCall(String reqUrl,String method) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Step 3rd:-
Create Activity for registration form and write following code.
Code for Reg:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter emailid"
android:id="@+id/txtemail"
></EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtpass"
android:hint="Enter password">
</EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter name"
android:id="@+id/txtname"
>
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnReg"
android:text="Register"
></Button>
</LinearLayout>
Code for Reg Code:-
package com.example.testapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
public class RegActivity extends AppCompatActivity {
String url= "https://shivaconceptdigital.com/api/reg.php";
EditText txtemail,txtpass,txtname;
String name,email,pass;
Button btn;
int status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
txtemail = findViewById(R.id.txtemail);
txtname = findViewById(R.id.txtname);
txtpass = findViewById(R.id.txtpass);
btn = findViewById(R.id.btnReg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name= txtname.getText().toString();
email = txtemail.getText().toString();
pass = txtpass.getText().toString();
url += "?fullname="+name+"&pwd="+pass+"&email="+email;
new GetReg().execute();
}
});
}
private class GetReg extends AsyncTask<Void ,Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url,"POST");
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
Log.e(null,jsonObj.toString());
status = jsonObj.getInt("status");
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(status==1)
{
Toast.makeText(getApplicationContext(),"Registered"+status+ url, Toast.LENGTH_LONG).show();
}
}
}
}Step:-Create Activity For Login FormCode for Login:-package com.example.testapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.nio.channels.AsynchronousChannelGroup;
public class LoginActivity13 extends AppCompatActivity {
EditText txtuname,txtupass;
Button btnlogin;
int status;
String url = "https://shivaconceptdigital.com/api/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login13);
txtuname = findViewById(R.id.txtuname1);
txtupass = findViewById(R.id.txtupass1);
btnlogin = findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = txtuname.getText().toString();
String pass = txtupass.getText().toString();
url+="?pwd="+pass+"&email="+email;
new GetLogin().execute();
}
});
}
class GetLogin extends AsyncTask<Void ,Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
}
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url,"POST");
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
status = jsonObj.getInt("status");
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(status==1)
{
Toast.makeText(getApplicationContext(),"Login Success and Activated"+status+ url,Toast.LENGTH_LONG).show();
}
else if(status==0)
{
Toast.makeText(getApplicationContext(),"Login Success and Not Activated"+status+ url,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Login Failed"+status+ url,Toast.LENGTH_LONG).show();
}
}
}
}Code for Design File:-<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity13">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtuname1"
android:hint="Enter Username">
</EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtupass1"
android:hint="Enter Password">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnlogin"
android:text="Login">
</Button>
</LinearLayout>I hope you have created registration and Login form using Webservices and implement into project.If any doubt then you can ask in comment section.
POST Answer of Questions and ASK to Doubt