Click here to Skip to main content
15,613,250 members
Articles / Mobile Apps / Android
Posted 23 Jul 2014


4 bookmarked

Call PHP webservice from Android

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Jul 2014CPOL4 min read


Here am trying to develop a web service using PHP and place it on remote server and calling it using Android app. This web service can be anything. it can be a simple calculation or it can complex like database updation. Here am showing the simple operation like addition, like we are passing 2 numbers from the application, the PHP web service has to accept and do calculation and return the result back to the application. Here am using a free webhost to host my web service.


This can be done using the SOAP (Simple Object access protocol). This communication is purely platform independent means this web service can be called from application. This is achieved because the communication is taking place through XML.  So wherever client makes a request it will taken in XML form and its entry will be checked in UDDI registry and it points the particular web service and execute it and responds to the client.  This is diagrammatically shown in the figure below

Image 1

So let’s look into an example, where we are we are calling a webservice hosted on the web server and calling it from the android app. Here we are taking example of simple application where it should return the price of a food material. So to that I am using a library tool called nusoap which you need to download from here, it has two folders called samples and lib…Lib is important for us.

Now register a web host of your choice, after getting access to your host, place the lib directory of nusoap into the public_html directory. So this set up the environment for the web service. Now we need to write the appropriate web service in php. First write a normal function in php call it a function.php

function price($name)
foreach($details as $n=>$p)
return $price;

This function has the array with the list of items and the cost of each. So whenever this function is called with the item name, its market price will be return. So this is just a function we have to make it accessible as the web service. Following code can do that call it service.php and the function name will be price.

require 'functions.php';
require 'lib/nusoap.php';
$server=new nusoap_server();


Now save all this into your public_html folder, now open this page in your browser, now open the WSDL link on your page you will get to see the XML data of the request which we are going to send.

Example: if your domain URL is http://www.{{mywebservicetest}}.com, if you stored your data in public_html, you can access them from your browser http://www.{{mywebservicetest}}.com/service.php, click the WSDL link there, you will get the XML communication data.

So now the web service is created and we can call it from anywhere from any platform. Now we should shift to Android platform and think about calling this service from Android phone…

We already defined the value for rice and jawar, now we need is just call this services and fetch  the result. As we are in android, first create the layout of the app, better keep the simple layout which can display the value returned from the web service. Lets call the activity_main.xml

tools:context=".MainActivity" >

android:textAppearance="?android:attr/textAppearanceMedium" />

Now create the activity class, to contact our web service we need to define four variables with its address and definition. They are SOAP_ACTION, NAMESPACE, METHOD_NAME

So in our application, it will something like this

String METHOD_NAME = "price";
String URL = "http://www.{{mywebservicetest}}.com/service.php?wsdl";
String SOAP_ACTION = "http://www.{{mywebservicetest}}.com/demourn:demo/price";
String NAMESPACE = "http://www.{{mywebservicetest}}.com/demourn:demo";

So now the address where to call the function and name of the function has been decalred. Now call them from the activity class. TO call it we need the soap source object

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
PropertyInfo fromProp =new PropertyInfo();

Now we need to decide which items price we want to check, I want to check the price value for the RICE, so I will pass it as a variable

String fromCurrency = "rice";
PropertyInfo fromProp =new PropertyInfo();

Create a envelope for this and pass it as a request to the PHP service, then will process it and produce a appropriate repsose i.e the cost of the rice.

Download Android Source code and check my application and try to modify with your requirements, in the function.php in the given function, you can add any logic in that section which can range from simple logic to database application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Question'maybe re-title the article to "Calling SOAP webservice from android"... Pin
l a u r e n24-Jul-14 22:28
l a u r e n24-Jul-14 22:28 
a lot of people use JSON instead of XML for communication between mobile devices and webservices due to its being way more lightweight, and SOAP can add even more overhead than plain old XML (POX? wow that sounds bad)

also, maybe a little more background on how the async communications work, and what the pros and cons of this approach are would help potential users of your ideas?

"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.