How to send a POST request with Google Volley on Android
In a Previous Tutorial, I have learned How To Get Data using Volley Library In the android app but now we Are Learning how to post Data on the server Using the volley Library in the android app.
lets now Start.
first making the MainActivity.xml file in our project like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="@color/Backgcolor"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".Registation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/Et_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/shapback"
android:hint="Name"
android:padding="10dp"
/>
<EditText
android:id="@+id/Et_Emailid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/shapback"
android:hint="Gmail id"
android:padding="10dp" />
<EditText
android:id="@+id/Et_phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shapback"
android:hint="Phone Number"
android:layout_marginBottom="15dp"
android:padding="10dp" />
<EditText
android:id="@+id/Et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/shapback"
android:hint="Password"
android:padding="10dp" />
<EditText
android:id="@+id/Et_confermpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/shapback"
android:hint="Conferm Password"
android:padding="10dp" />
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/buttionshap"
android:padding="10dp"
android:text="Registation"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold"
android:textAllCaps="false"/>
</LinearLayout>
</ScrollView>
Note : volley Library SinglationClass and Dependancies copy our previous tutrial
now make MainActivity.java Class In our project.
package com.bspl.qscanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bspl.qscanner.extraclass.VolleySingleton;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class MainActivity extends Activity {
Button Registation;
EditText name,emailid,phonenumbe,password,confermpassword;
String str_name,str_emailid,str_phonenumbe,str_password,str_confermpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registation);
Registation=findViewById(R.id.bt_login);
name=findViewById(R.id.Et_Name);
emailid=findViewById(R.id.Et_Emailid);
phonenumbe=findViewById(R.id.Et_phonenumber);
password=findViewById(R.id.Et_password);
confermpassword=findViewById(R.id.Et_confermpassword);
Registation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
volidation();
}
});
}
private void volidation() {
str_name=name.getText().toString();
str_emailid=emailid.getText().toString();
str_phonenumbe=phonenumbe.getText().toString();
str_password=password.getText().toString();
str_confermpassword=confermpassword.getText().toString();
if(str_name.equals("")){
name.setError("Please Enter Name");
}
else if(str_emailid.equals("")){
emailid.setError("Please Enter Email Id");
}
else if(str_phonenumbe.equals("")){
phonenumbe.setError("Please Enter Phone Number");
}
else if(str_password.equals("")){
password.setError("Please Enter Password");
}
else if(!str_confermpassword.equals(str_password)){
name.setError("Password is not match ");
}
else {
registerUser();
Toast.makeText(this, "Now Form is valid"+str_name, Toast.LENGTH_SHORT).show();
}
}
private void registerUser() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, "your registation Api",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting response to json object
Log.d("responce", response);
Object obj = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("responce",error.toString());
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
// params.put("Content-Type", "application/json");
params.put("Name",str_name);
params.put("Email",str_emailid);
params.put("Number",str_phonenumbe);
params.put("Pasword",str_password);
Log.e("Anil",params.toString());
return new JSONObject(params).toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
}
Comments
Post a Comment