Sun. Nov 24th, 2024

API:

URL: https://androindian.com/test/Register_raw.php

Request:

{“username”:“Raj”,“password”:“1234”,“email”:“Rajtest2356178@admin}

Response:

{“key”:”You are registered successfully.”,”status”:”success”}

{“key”:”Email already Exists”,”status”:”failed”}

build.gradle

implementation ("com.squareup.retrofit2:converter-gson:2.4.0")
    implementation ("com.squareup.retrofit2:retrofit:2.4.0")
    implementation ("com.google.code.gson:gson:2.8.9")

activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<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=".MainActivity"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register here"
         />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="UserName"
        android:layout_margin="5dp">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:layout_margin="5dp">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/passwod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_margin="5dp">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </com.google.android.material.textfield.TextInputLayout>

        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Register"
            android:layout_gravity="center"
            />
    </LinearLayout>
    </androidx.cardview.widget.CardView>

</RelativeLayout>
</layout>

MainActivity.kt

package com.androindian.project7pm

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.androindian.project7pm.databinding.ActivityMainBinding
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {

    var binding: ActivityMainBinding?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this,R.layout.activity_main)

        binding?.register?.setOnClickListener{
            var name=binding?.username?.editText?.text.toString().trim()
            var email=binding?.email?.editText?.text.toString().trim()
            var password=binding?.passwod?.editText?.text.toString().trim()

            var jsonObject=JSONObject()
           // jsonObject.put("key is from api","valuee from xml")
            jsonObject.put("username",name)
            jsonObject.put("password",password)
            jsonObject.put("email",email)

            var retrofit=Retrofit.Builder().
                    baseUrl("https://androindian.com/test/")
                .addConverterFactory(GsonConverterFactory.create()).build()

            var retroInterface=retrofit.create(SampleProjectInterface::class.java)

            var regisetercall: Call<RegisterResponse>

            val `xyz` = JsonParser().parse(jsonObject.toString()).asJsonObject

            //8
            val regResponseCall = retroInterface.createUser(`xyz`)

            regResponseCall?.enqueue(object : retrofit2.Callback<RegisterResponse?>{
                override fun onResponse(
                    call: Call<RegisterResponse?>,
                    response: Response<RegisterResponse?>
                ) {
                    var res=response.body()?.status
                    if(res.equals("failed")){
                        var res1=response.body()?.key
                        Toast.makeText(this@MainActivity,""+res1,Toast.LENGTH_LONG).show()

                    }else{
                        var res1=response.body()?.key
                        Toast.makeText(this@MainActivity,""+res1,Toast.LENGTH_LONG).show()
                        var intent= Intent(this@MainActivity, Login::class.java)
                        startActivity(intent)
                    }
                }

                override fun onFailure(call: Call<RegisterResponse?>, t: Throwable) {
                    Toast.makeText(this@MainActivity,""+t.toString(),Toast.LENGTH_LONG).show()
                }
            })
        }
    }
}

SampleProjectInterface.kt

package com.androindian.project7pm

import com.google.gson.JsonObject
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.POST

 public interface SampleProjectInterface {


    @Headers("Content-Type: application/json")
    @POST("Register_raw.php")
    fun createUser(@Body jsonObject: JsonObject): Call<RegisterResponse>?

     
}

RegisterResponse.kt

package com.androindian.project7pm

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

class RegisterResponse {
    @SerializedName("key")
    @Expose
    var key: String? = null

    @SerializedName("status")
    @Expose
    var status: String? = null
}

androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

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