Friday, February 24, 2012

Pin It


Get Gadget

Accessing Apache Axis2 Web service inside a Android Application - Part 1 - Apache Axis 2, Android API 8, phpmyadmin, Eclipse Indigo, VMware Player

Part 1

In this series of articles I am going to show you the complete set of steps to access a web service inside Android application. I am expecting you to have knowledge about following,
  • Create a web service using Apache Axis 2 and Eclipse. (If you don't know here are some useful links : Link1 , Link 2)
  • Create a simple web service client using Axis2 plug in and Eclipse. (If you don't know here are some useful links : Link1)
  • Create a simple android project with text fields to show some texts. (If you don't know here are some useful links : Link1)
  • More importantly you have to have a working virtual machine inside  your machine using something like VMware player. Also you have to have a host only private network with this virtual machine and you machine. Also you have to install Eclipse,Apache axis 2 and tomcat server in this virtual machine.
So having those prerequisites I will continue like this.I am going to create a simple web service which is capable to give data to it users by querying a local database it has.

Step 1- In my example I use a database called travel_ceylon. So In side your virtual machine create a database using phpmyadmin. and import following sql file to that database,

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS `category` (
  `Category` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `category` (`Category`) VALUES
('History'),
('Religon - Buddhisum'),
('Religon - Catholic'),
('Religon - Islam'),
('Religon - Hindu'),
('Technology'),
('Nature');

CREATE TABLE IF NOT EXISTS `city` (
  `City_Name` varchar(100) NOT NULL,
  `Latitude` float NOT NULL,
  `Longitude` float NOT NULL,
  PRIMARY KEY (`City_Name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;


INSERT INTO `city` (`City_Name`, `Latitude`, `Longitude`) VALUES
('Aluthgama', 6.59991, 79.9568),
('Ambalangoda', 6.2367, 80.0544),
('Matara', 5.94413, 80.5494),
('Monaragala', 6.87268, 81.3506),
('Moratuwa', 6.77453, 79.8826),
('Wellawaya', 6.73203, 81.1019);

CREATE TABLE IF NOT EXISTS `close_to` (
  `City_Name` varchar(100) NOT NULL,
  `Place_Name` varchar(100) NOT NULL,
  `Distance` float NOT NULL,
  KEY `City_Name` (`City_Name`,`Place_Name`),
  KEY `Place_Name` (`Place_Name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

CREATE TABLE IF NOT EXISTS `important_places` (
  `Place_Name` varchar(100) NOT NULL,
  `Category` varchar(200) NOT NULL,
  `Description` varchar(500) NOT NULL,
  `Latitude` float NOT NULL,
  `Longitude` float NOT NULL,
  PRIMARY KEY (`Place_Name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

INSERT INTO `important_places` (`Place_Name`, `Category`, `Description`, `Latitude`, `Longitude`) VALUES
('Galle Face', 'Lesuire,History', 'Park', 6.92496, 79.8446),
('Gangaramya Temple', 'Buddhisum,History,Religon', 'A Big Temple in Colombo', 6.91625, 79.8563),
('Viharamahadevia Park', 'Lesuire', 'Park', 6.91379, 79.8626),
('Weherahena Temple', 'Buddhisum,History,Religon', 'Weherahena Temple with a hugh Buddha Satchive', 5.95313, 80.5759);

CREATE TABLE IF NOT EXISTS `important_places_for_approval` (
  `Place_Name` varchar(100) NOT NULL,
  `Category` varchar(150) NOT NULL,
  `Description` varchar(500) NOT NULL,
  `Longitude` float NOT NULL,
  `Latitude` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE `close_to`
  ADD CONSTRAINT `close_to_ibfk_1` FOREIGN KEY (`City_Name`) REFERENCES `city` (`City_Name`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `close_to_ibfk_2` FOREIGN KEY (`Place_Name`) REFERENCES `important_places` (`Place_Name`) ON DELETE CASCADE ON UPDATE CASCADE;

Step 2 - Create a Dynamic Web Project using Eclipse inside your virtual machine. Put this as your web service definition,

package org.web.travel_ceylon.ws;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Travel_Ceylon_Web_Service {
 private String url;
 private Connection con;
 private Statement stmt;

 public void connectToDB() {
  url = "jdbc:mysql://localhost:3306/travel_ceylon";
  try {
   Class.forName("com.mysql.jdbc.Driver");
   con = (Connection) DriverManager.getConnection(url, "root", "");
  } catch (Exception e) {
   System.out.println("Error - Unable to Connect to the Database" + e);

  }
 }

 public float getLongitude_City(String city) {
  connectToDB();
  float lngt = 0;
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Longitude FROM city WHERE City_Name='"
       + city + "'");
   rs.next();
   lngt = rs.getFloat("Longitude");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get longitude of " + city
     + " :" + e);
  }
  return lngt;
 }

 public float getLatitude_City(String city) {
  connectToDB();
  float latt = 0;
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Latitude FROM city WHERE City_Name='"
       + city + "'");
   rs.next();
   latt = rs.getFloat("Latitude");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get latitude of " + city
     + " :" + e);
  }
  return latt;
 }

 public float getLongitude_Im_Place(String place) {
  connectToDB();
  float lngt = 0;
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Longitude FROM important_places WHERE Place_Name='"
       + place + "'");
   rs.next();
   lngt = rs.getFloat("Longitude");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get longitude of " + place
     + " :" + e);
  }
  return lngt;
 }

 public float getLatitude_Im_Place(String place) {
  connectToDB();
  float latt = 0;
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Latitude FROM important_places WHERE Place_Name='"
       + place + "'");
   rs.next();
   latt = rs.getFloat("Latitude");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get latitude of " + place
     + " :" + e);
  }
  return latt;
 }

 public String getCategory_Im_Place(String place) {
  connectToDB();
  String cat = "";
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Category FROM important_places WHERE Place_Name='"
       + place + "'");
   rs.next();
   cat = rs.getString("Category");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get  Category of " + place
     + " :" + e);
  }
  return cat;
 }

 public String getDescription_Im_Place(String place) {
  connectToDB();
  String des = "";
  try {
   stmt = (Statement) con.createStatement();
   ResultSet rs = stmt
     .executeQuery("SELECT Description FROM important_places WHERE Place_Name='"
       + place + "'");
   rs.next();
   des = rs.getString("Description");
  } catch (SQLException e) {
   System.out.println("Error - Unable to get Description of " + place
     + " :" + e);
  }
  return des;
 }
} 

Step 3 - Run your web service project in the tomcat server on eclipse. Yo will get your WSDL file like this


You know that this web service running inside the virtual machine you have. Also As I explained earlier you have host only network between virtual machine and your machine. So this web service can be access via your original machine. To verify that please enter this in your web browserhttp://Virtual Machines IP address:8080/Travel_Ceylon_Central_Web_Service/services/Travel_Ceylon_Web_Service?wsdl. In my example it was this. http://192.168.177.130:8080/Travel_Ceylon_Central_Web_Service/services/Travel_Ceylon_Web_Service?wsdl  Then you can see this in your web browser.



So know you have a web service which can be accessible through a network. So lets proceed to the part of creating 
Android Client which use this web service. I will explain it in my next post.

1 comment: