Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Call PHP webservice from Android

0.00/5 (No votes)
23 Jul 2014CPOL4 min read 37K   1.6K  

Introduction

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.

Details

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

<?php
function price($name)
{
$details=array(
'rice'=>100,
'jawar'=>200
);
foreach($details as $n=>$p)
{
if($name==$n)
$price=$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.

<?php
require 'functions.php';
require 'lib/nusoap.php';
$server=new nusoap_server();
$server->configureWSDL("demo"."urn:demo");
$server->register("price",
array("name"=>'xsd:string'),
array("return"=>'xsd:integer'));

$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:'';
$server->service($HTTP_RAW_POST_DATA);
?>

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

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="60dp"
android:text=""
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
URL.

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();
fromProp.setName("name");
fromProp.setValue(fromCurrency);

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.

License

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