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.java
package com.androindian.volley;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
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;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
String url = "https://androindian.com/test/Register.php";
//2
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(
MainActivity.this, R.layout.activity_main);
//3
requestQueue = Volley.newRequestQueue(MainActivity.this);
binding.Reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", binding.name.getText().toString().trim());
jsonObject.put("email", binding.email.getText().toString().trim());
jsonObject.put("password", binding.Pass.getText().toString().trim());
} catch (JSONException e) {
e.printStackTrace();
}
//4
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, jsonObject
, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//8
//Toast.makeText(MainActivity.this, response.getString("key").toString(), Toast.LENGTH_SHORT).show();
try {
String res = response.getString("status");
if (res.equalsIgnoreCase("failed")) {
String res1 = response.getString("key");
Toast.makeText(MainActivity.this, res1, Toast.LENGTH_SHORT).show();
} else if (res.equalsIgnoreCase("success")) {
String res1 = response.getString("key");
Toast.makeText(MainActivity.this, res1, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//5
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//7
Toast.makeText(MainActivity.this, "" + 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>
Bulid.gradle
dataBinding{
enabled=true
}
}
dependencies {
implementation 'com.android.volley:volley:1.2.1'
}