Sending Contents from Remote Server
API to Receive and Insert Data
setdata.php
<?php
echo "{'message':'Data Received Successfully'}";
?>
Send data from APP
myactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Name"
android:id="@+id/edtName"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Address"
android:id="@+id/edtAddress"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="Send Data"
android:id="@+id/btnSend"
/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
Button btnSend;
EditText edtName,edtAddress;
@Override
protected void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.myactivity);
btnSend=findViewById(R.id.btnSend);
edtName=findViewById(R.id.edtName);
edtAddress=findViewById(R.id.edtAddress);
btnSend.setOnClickListener(view->{
volleyRequest();
});
}
public void volleyRequest(){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
//url for localhost
String url =http://10.0.2.2/bcaapi/setdata.php?name=
+name+”&&address=”+address;
// Request a string response from the provided URL.
StringRequest stringRequest = new
StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//response here
Toast.makeText(MyActivity.this, response,
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//displaying error response message
Log.d("exception",error.toString());
} })
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
}
Output