Part 1 : Calling a webservice from Android Application

Calling a webservice from Android app

There are two parts

Part 1: Creating a WSDL Service.

Part 2: Calling A WEB SERVICE from Android App.

——————————————————————————————————

Part 1: Creating a webservice

——————————————————————————————————

  • Prerequisite: Eclipse which supports web development.
  • Download Axis2-1.4 from http://ws.apache.org/axis2/download/1_4/download.cgi
  • Download the zip version of “Standard Binary Distribution”.
  • Extract in the desired folder and keep a note of its location.
  • Open Eclipse. Go to Window -> Preference and you will get the following screen

  • Click on “Web Services -> Axis2 Preferences”
  • Browse the directory to the location, where we have extracted The “axis2-1.4”
  • We should get the following screen “Axis2 runtime loaded sucessfully”.

  • Click “OK”
  • Create a new “Dynamic Web Project”

  • Make sure Server is configured Properly.
  • Click Finish.
  • Create a new package. Right Click on “src -> New -> Package “

  • Click Finish.
  • Create a new Java class inside the same Package.

  • Click Finish.
  • package org.web.backend.calculator;
  • Create class “Calculate” inside the package, with following methods.
    public class Calculate {
    public int sum(int i, int j) {
    return (i + j);
    }
    public int subtract(int i, int j) {
    return (i – j);
    }public int multiply(int i, int j) {
    return (i * j);
    }

    public int divide(int i, int j) {
    return (i / j);
    }
    }

  • Save the file.
  • When services are deployed to Axis, the wsdd file is used during the deployment and its contents end up within server-config.wsdd in /usr/local/OMII/jakarta-tomcat-5.0.25/webapps/axis/WEB-INF.
  • Since we dont have such file, we have to create a “server-config.wsdd”
  • Right Click on “WEB-INF” and select “New -> File”. Name it as “server-config.wsdd”.
  • The contents can copy pasted from this link
  • Make sure you have the following jar files inside the library of created Project.
  1. activation.jar
  2. commons-discovery.jar
  3. jaxrpc.jar
  4. mail.jar
  5. saaj.jar
  • These jar files are required for successful completeion of creation of WSDL service. So if you dont have kindly download it inside the lib folder of axis2-1.4, as shown below.

er

  • So now we to copy paste all the library from bin folder inside the lib of project
  • From : C:\Users\j3 d patel\jars\Axis2\axis2-1.4\lib
  • To : WEB-INF\lib
  • Now Project Explorer looks like the following screen shot

  • Right Click the Java Class we created “Calculate.java”.
  • New -> WebService.

  • Click Next
  • Click on Web Service runtime : Apache Axis
  • Select Apache Axis 2 and click OK. Finally it should look like the foloowing screen shot.

  • Select Next.

  • Select Next and then click “Start Server.”
  • Click “Next”

  • If you get the following screen, everything is working fine.
  • Click on Services.

  • If you get the above screen, we have sucessfully created a wsdl file. The only step Remaining is to click on “Calculate”.
  • If it points to “http://localhost:8080/AndroidBackend/services/Calculate?wsdl” with a wsdl file in your web browser, We have successfully created a wsdl file for our android app.
  • Keep a note of this wsdl url as it will be required in Part 2.

Go to : Part 2         : For calling webservice from ANDROID

——————————————————–

For calling webservice from Flex application

<fx:Declarations>
<!– Place non-visual elements (e.g., services, value objects) here –>
<s:WebService id=”calculatorService”
wsdl=”http://localhost:8080/AndroidBackend/services/Calculate?wsdl&#8221;
result=”calculatorService_resultHandler(event)”
fault=”calculatorService_faultHandler(event)”/>

</fx:Declarations>
<s:Panel x=”75″ y=”87″ width=”312″ height=”260″ title=”Calculator”>
<s:Button id=”add” x=”15″ y=”163″ width=”53″ label=”+”
click=”calculatorService.sum(Number(number1.text),Number(number2.text));”/>

<s:Button id=”divide” x=”211″ y=”163″ width=”53″ label=”/”
click=”calculatorService.divide(Number(number1.text),Number(number2.text));”/>

<s:Button id=”multiply” x=”148″ y=”163″ width=”53″ label=”*”
click=”calculatorService.multiply(Number(number1.text),Number(number2.text));”/>

<s:Button id=”subtract” x=”81″ y=”163″ width=”53″ label=”-“
click=”calculatorService.subtract(Number(number1.text),Number(number2.text));”/>

 

 

 

About these ads

15 thoughts on “Part 1 : Calling a webservice from Android Application

  1. Hi there !

    I would like to know how can I deploy the webservice on my remote server running on ubuntu.
    Apache tomcat is already installed, but i don’t know i can put it on my remote server from eclipse.

    Thanks.

  2. Hey good work.. helped me lot..
    Can u please help me about this..
    I have created one signup page in android and it contains some text boxes for names and also it contains one upload button throgh this ihave to upload image from android gallery and have to store it in mysql.
    How can I store image into mysql using web services?
    I appreciate your help..
    Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s