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.webviewexample;
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 response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// 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;
}
public String makeServiceCall1(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// 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=".MainActivity3">
<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.webviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class MainActivity3 extends AppCompatActivity {
String url= "https://shivaconceptsolution.com/webservices/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_main3);
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 += "?name="+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.makeServiceCall1(url);
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(),"Registered"+status+ url,Toast.LENGTH_LONG).show();
}
}
}
}Step:-Create Activity For Login FormCode for Login:-package com.example.webviewexample;
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;
public class MainActivity4 extends AppCompatActivity {
String url= "https://shivaconceptsolution.com/webservices/login.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_main4);
txtemail = findViewById(R.id.txtemail1);
txtpass = findViewById(R.id.txtpass1);
btn = findViewById(R.id.btnReg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
email = txtemail.getText().toString();
pass = txtpass.getText().toString();
url += "?pwd="+pass+"&email="+email;
new GetLogin().execute();
}
});
}
private class GetLogin 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.makeServiceCall1(url);
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"+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"
tools:context=".MainActivity4">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter emailid"
android:id="@+id/txtemail1"
></EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtpass1"
android:hint="Enter password">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnReg"
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 a Comment
POST Answer of Questions and ASK to Doubt