1) Add this dependency on your build.gradle(Module:app)
implementation 'com.android.volley:volley:1.1.0'
2) Add internet permission on your Manifest.xml file
<uses-permission android:name="android.permission.INTERNET"/>
3) MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name"
android:layout_marginTop="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Job"
android:id="@+id/et_job"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_submit"
android:text="Submit"
android:layout_marginTop="10dp"/>
</LinearLayout>
4) MainActivity.java
public class MainActivity extends AppCompatActivity {
private RequestQueue requestQueue;
private EditText et_name, et_job;
private Button btn_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText)findViewById(R.id.et_name);
et_job = (EditText)findViewById(R.id.et_job);
btn_submit = (Button) findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = "{"+
"\"name\":" + "\"" + et_name.getText().toString() + "\","+
"\"job\":" + "\"" + et_job.getText().toString() + "\""+
"}";
Submit(data);
}
});
}
private void Submit(String data)
{
final String savedata= data;
String URL="https://YOUR_API_URL";
requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject objres=new JSONObject(response);
Toast.makeText(getApplicationContext(),objres.toString(),Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),"Server Error",Toast.LENGTH_LONG).show();
}
//Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
//Log.v("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return savedata == null ? null : savedata.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
//Log.v("Unsupported Encoding while trying to get the bytes", data);
return null;
}
}
};
requestQueue.add(stringRequest);
}
}