Implement Register RestApi Using Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.4.0' // This is use for Retrofit.
implementation 'com.squareup.retrofit2:converter-gson:2.4.0' // This is use for Covert to gson.
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1' // This Is use for log checking.
Activity_register.xml
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".Register">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cafe Coffee "
android:layout_marginTop="130dp"
android:gravity="center"
android:textSize="70dp"
android:fontFamily="cursive"
android:textColor="#ED2067"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:id="@+id/nameLayout"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/etName"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="@+id/emaillayout"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_below="@+id/nameLayout"
android:layout_marginTop="10dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:id="@+id/etEmail"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="@+id/passwordlayout"
android:layout_below="@+id/emaillayout"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/black"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:id="@+id/etPassword"
/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textColor="@color/white"
android:background="#3BB5EC"
android:layout_below="@id/passwordlayout"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Already Registster |
Login Here"
android:layout_below="@+id/btnRegister"
android:gravity="center_horizontal"
android:layout_marginTop="5dp"
android:id="@+id/loginlink"/>
</RelativeLayout>
Create RegisterResponse.java
for get Register Response.
package
com.codewithandroid.fulldemo;
public class
RegisterResponse {
String error;
String message;
public RegisterResponse(String error,
String message) {
this.error = error;
this.message = message;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Create Api
Interface Class
package
com.codewithandroid.fulldemo;
import
com.codewithandroid.fulldemo.ModelResponse.RegisterResponse;
import
retrofit2.Call;
import
retrofit2.http.Field;
import
retrofit2.http.FormUrlEncoded;
import
retrofit2.http.POST;
public
interface Api {
@FormUrlEncoded
@POST("register.php")
Call<RegisterResponse> register(
@Field("username") String
username,
@Field("email") String email,
@Field("password") String
password
);
}
Create
RetrofitClient.java Class.
package com.codewithandroid.fulldemo;
import
okhttp3.OkHttpClient;
import
okhttp3.logging.HttpLoggingInterceptor;
import
retrofit2.Retrofit;
import
retrofit2.converter.gson.GsonConverterFactory;
public class
RetrofitClient {
private static String
BASE_URL="http://anujsinha.ml/UserManagementApi/";
private static RetrofitClient
retrofitClient;
private static Retrofit retrofit;
private OkHttpClient.Builder builder = new
OkHttpClient.Builder();
private HttpLoggingInterceptor interceptor
= new HttpLoggingInterceptor();
private RetrofitClient(){
interceptor.level(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(builder.build())
.build();
}
public static synchronized RetrofitClient
getInstance(){
if (retrofitClient==null){
retrofitClient = new
RetrofitClient();
}
return retrofitClient;
}
public Api getApi(){
return retrofit.create(Api.class);
}
}
Register.Java
package
com.codewithandroid.fulldemo;
import
androidx.appcompat.app.AppCompatActivity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Patterns;
import
android.view.View;
import
android.view.WindowManager;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.codewithandroid.fulldemo.ModelResponse.RegisterResponse;
import
org.w3c.dom.Text;
import
java.util.regex.Pattern;
import
retrofit2.Call;
import
retrofit2.Callback;
import
retrofit2.Response;
public class
Register extends AppCompatActivity implements View.OnClickListener {
TextView loginLink;
EditText name,email,password;
Button register;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//hide Actionbar
getSupportActionBar().hide();
//hide Stataus bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
loginLink = findViewById(R.id.loginlink);
name = findViewById(R.id.etName);
email = findViewById(R.id.etEmail);
password =
findViewById(R.id.etPassword);
register =
findViewById(R.id.btnRegister);
loginLink.setOnClickListener(this);
register.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnRegister:
registerUser();
//Toast.makeText(this,
"register", Toast.LENGTH_SHORT).show();
break;
case R.id.loginlink:
swithOnLogin();
break;
}
}
private void registerUser() {
String userName =
name.getText().toString();
String userEmail =
email.getText().toString();
String userPassword =
password.getText().toString();
if (userName.isEmpty()){
name.requestFocus();
name.setError("Please Enter
Name");
return;
}
if (userEmail.isEmpty()){
email.requestFocus();
email.setError("Please Enter
Email");
return;
}
if
(Patterns.EMAIL_ADDRESS.matcher(userEmail).matches()){
email.requestFocus();
email.setError("Please Enter
Correct Email");
return;
}
if (userPassword.isEmpty()){
password.requestFocus();
password.setError("Please
Enter Password");
return;
}
if (userPassword.length()<8){
password.requestFocus();
password.setError("Please
Enter Password");
return;
}
Call<RegisterResponse> call =
RetrofitClient
.getInstance()
.getApi()
.register(userName,userEmail,userPassword);
call.enqueue(new
Callback<RegisterResponse>() {
@Override
public void
onResponse(Call<RegisterResponse> call, Response<RegisterResponse>
response) {
RegisterResponse
registerResponse = response.body();
if (response.isSuccessful()){
Intent intent = new
Intent(Register.this,Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
Toast.makeText(Register.this, registerResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Register.this, registerResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void
onFailure(Call<RegisterResponse> call, Throwable t) {
}
});
}
private void swithOnLogin() {
startActivity(new Intent(
Register.this,Login.class));
}
}
OUTPUT
If you Create Rest Api In PHP So Follow This Step.
First Step
Create Connection.php File and Paste this Code.
<?php
$hostName='localhost';
$userName='root';
$userPass='';
$dbName='userdata';
$con=mysqli_connect($hostName,$userName,$userPass,$dbName);
/*if(!$con){
echo "connection failed";
}
else
echo "connection succes";*/
?>
Second Step Create Login.php File and Paste this Code.
<?php
$username=$_POST['username'];
$email=$_POST['email'];
$password=md5($_POST['password']);
$checkUser="SELECT * from user WHERE email='$email'";
$checkQuery=mysqli_query($con,$checkUser);
if(mysqli_num_rows($checkQuery)>0){
$response['error']="002";
$response['message']="User exist";
}
else
{
$insertQuery="INSERT INTO user(username,email,password) VALUES('$username','$email','$password')";
$result=mysqli_query($con,$insertQuery);
if($result){
$response['error']="000";
$response['message']="Register successful!";
}
else
{
$response['error']="001";
$response['message']="Registeration failed!";
}
}
echo json_encode($response);
?>
1 Comments
How To Make Money On Sports Betting
ReplyDeleteOnline sports betting หารายได้เสริม is available for a whole goyangfc host of US 바카라 and European sports betting markets. https://vannienailor4166blog.blogspot.com/ Some US states, like Louisiana and New 토토 사이트 모음 Jersey, allow