Lab Detail


Sno Back Back Subject subject date title note
1 1 Back to subject Mobile Application Development Lab - 20A05706 (Lab) Aug. 12, 2025 Do CURD Operation Using API Via Android sdk show Data from Server Send Points

reference Link Code : https://androindian.com/registration-using-retrofit-raw-data/

 

Do CURD Operation Using API Via Android sdk show Data from Server Send Points 

Do POST Operation 

 

build.gradle

 

E:\android\android apps\APIIntegration\app\src\main\AndroidManifest.xml

    dataBinding{
        enable=true
    }

dependencies {
...................

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")
}

E:\android\android apps\APIIntegration\app\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.APIIntegration"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

E:\android\android apps\APIIntegration\app\src\main\res\layout\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:id="@+id/main"
        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>

E:\android\android apps\APIIntegration\app\src\main\java\com\example\apiintegration\MainActivity.kt

package com.example.apiintegration
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import com.example.apiintegration.databinding.ActivityMainBinding
import com.google.gson.JsonParser
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.Response
import android.util.Log

class MainActivity : AppCompatActivity() {

    var binding: ActivityMainBinding?=null
    val TAG = "MainActivity"
    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("endpoint/api/")
                .addConverterFactory(GsonConverterFactory.create()).build()

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

            var regisetercall: Call<RegisterResponse>

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

            Log.d(TAG, "Request JSON: $xyz")

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

            regResponseCall?.enqueue(object : Callback<RegisterResponse?>{
                override fun onResponse(
                    call: Call<RegisterResponse?>,
                    response: Response<RegisterResponse?>

                ) {
                    Log.d(TAG, "Response received: $response,$call")
                    if (response.isSuccessful && response.body() != null) {
                        val registeredUsername = response.body()?.username
                        Toast.makeText(this@MainActivity, "Registered: $registeredUsername", Toast.LENGTH_LONG).show()

                      // Navigate to Login activity or next step if needed
                     // startActivity(Intent(this@MainActivity, LoginActivity::class.java))
                    } else {
                        Toast.makeText(this@MainActivity, "Registration failed: ${response.errorBody()?.string()}", Toast.LENGTH_LONG).show()
                    }

                //                    Toast.makeText(this@MainActivity,""+response.body(),Toast.LENGTH_LONG).show()
//                    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) {
                    Log.d(TAG, "OnFailure: $call,$t")
                    Toast.makeText(this@MainActivity,""+t.toString(),Toast.LENGTH_LONG).show()
                }
            })

//        enableEdgeToEdge()
//        setContentView(R.layout.activity_main)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
    }

    }
}

E:\android\android apps\APIIntegration\app\src\main\java\com\example\apiintegration\SampleProjectInterface.kt

package com.example.apiintegration

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/")
    fun createUser(@Body jsonObject: JsonObject): Call<RegisterResponse>?


}

 

E:\android\android apps\APIIntegration\app\src\main\java\com\example\apiintegration\RegisterResponse.kt

package com.example.apiintegration

//https://www.jsonschema2pojo.org/  1st do it here json in website to do convert java to kt next 
//✅ 1. Ensure You're Using the Right Shortcut
//For Windows/Linux:
//Ctrl + Alt + Shift + K


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


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