Click here to Skip to main content
Click here to Skip to main content
Go to top

Call PHP webservice from Android

, 23 Jul 2014
Rate this:
Please Sign up or sign in to vote.

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

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)

Share

About the Author

sandeepten910

United States United States
No Biography provided

Comments and Discussions

 
Question'maybe re-title the article to "Calling SOAP webservice from android"... Pinmemberl a u r e n24-Jul-14 22:28 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 23 Jul 2014
Article Copyright 2014 by sandeepten910
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid