Wed. Apr 17th, 2024

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <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="com.androindian.retrofit.MainActivity">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Name"
            android:id="@+id/name"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Email"
            android:id="@+id/email"/> <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Pass"
        android:id="@+id/Pass"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Reg"
            android:id="@+id/Reg"/>


    </LinearLayout>
</layout>

MainActivity.kt

package com.androindian.volley

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.androindian.volley.databinding.ActivityMainBinding
import org.json.JSONException
import org.json.JSONObject

class MainActivity : AppCompatActivity() {
    var binding: ActivityMainBinding? = null
    var url = "https://androindian.com/test/Register.php"

    //2
    var requestQueue: RequestQueue? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(
            this@MainActivity, R.layout.activity_main
        )

        //3
        requestQueue = Volley.newRequestQueue(this@MainActivity)
        binding?.Reg?.setOnClickListener { //1
            val jsonObject = JSONObject()
            try {
                jsonObject.put("username", binding?.name?.text.toString().trim { it <= ' ' })
                jsonObject.put("email", binding?.email?.text.toString().trim { it <= ' ' })
                jsonObject.put("password", binding?.Pass?.text.toString().trim { it <= ' ' })
            } catch (e: JSONException) {
                e.printStackTrace()
            }

            //4
            val jsonObjectRequest = JsonObjectRequest(
                Request.Method.POST, url, jsonObject, { response -> //8
                    //Toast.makeText(MainActivity.this, response.getString("key").toString(), Toast.LENGTH_SHORT).show();
                    try {
                        val res = response.getString("status")
                        if (res.equals("failed", ignoreCase = true)) {
                            val res1 = response.getString("key")
                            Toast.makeText(this@MainActivity, res1, Toast.LENGTH_SHORT).show()
                        } else if (res.equals("success", ignoreCase = true)) {
                            val res1 = response.getString("key")
                            Toast.makeText(this@MainActivity, res1, Toast.LENGTH_SHORT).show()
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                } //5
            ) { error -> //7
                Toast.makeText(this@MainActivity, "" + error.toString(), Toast.LENGTH_SHORT).show()
            }

            //6
            requestQueue!!.add(jsonObjectRequest)
        }
    }
}

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"></uses-permission>

    <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:supportsRtl="true"
        android:theme="@style/Theme.Volley"
        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>

build.gradle

dataBinding{
        enabled=true
    }
}

dependencies {

   
    implementation 'com.android.volley:volley:1.2.1'
}

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 *