In this post we are going to create a simple Android application in which we can send data to PHP webservice and get response from that service. In my last post i explain you how to read data from webservice and display in our android application. Today most of application used internet to send information and get updates from server.
A brief introduction what we are going to create. In this app first we create a EditText field where we write some thing and one button. When user press button we send EditText field data to service where we get posted data from application and we also perform (CRUD) operation but i simply echo that data and return same data as a response.

Start your New Android Project

1. Create a new project in Eclipse from File ⇒ New ⇒ Android Application Project. I had left my main activity name as .

2. As we are Send and Recieve the JSON by making HTTP calls, we need to add INTERNET permission in our AndroidManifest.xml file. Open AndroidManifest.xml and add the following permission.

3. Now open your activity_main.xml inside res ⇒ layout folder and create a EditText and button through which we can write and send data to webservice. And display result.

4. Now open your inside src ⇒ In this file we send data to webservice and get response note that i also use some extra jar file (httpclient-4.1-beta1.jar) to make my app work .

In above java code i create a AsyncTask to run this process and inside i call DefaultHttpClient() and call HttpPost() to send data in post method and create json object and pass my EditText field data to webservice. And data is send to server we get some response and convert that response to string using EntityUtils and save it to string variable.

Import jar to your project

Right click on your project folder inside eclipse ⇒ Build Path ⇒Add External Archives Browser external jar you want to use and select them. Now again right click on your project and go to Properties ⇒ Java Build Path ⇒ Order and Export.

PHP WebService

This is simple php code to return the get data.

Now your application is  ready to test.