Ads 468x60px

For New Update Use this Link Offer for you


Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Wednesday, 17 July 2013

How to call web service from android application

create simple drop down menu using jquery
Introduction : -

 In this  article i am explaining how to call the  asmx web service .This is very nice article for call the asmx web  service for android application.
Setp 1 ->  Open visual studio and create a new project like this

And write the name of project and click ok

Step 2  -> Write click on project  add ->add new item
select  web service and click ok and write the code as shown below on our webservice.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components


    public int Add(int a, int b)
        return a + b;


Step 3 -> Run our web service and check the output is proper or not  like this
Click on Add

Enter Values  a=10 and b=10

Then Click on Invoke and output as shown below

Start Android Applcation : -

step 1 -> Doble Click on eclipse IDE and  in front of you appear the GUI .
step 2 -> select File->new->Android Application Project
and fill all the filed as show in image

step 3-> Click Next->Next->Next->Next->Finish
Step 4-> On Android Application is Created

Step4->Download the jar file

Step 5 -> Copy and past in libs folder and write click->Build Path->Configration Build Path

Note - If libs  folder not avable in our project then create new folder name like libs and keep this jar file

step 6 -> Now Write Click on  Project Add->New->Class  [class name like]

And Write the Code as Shown Below 

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap {
    public final String SOAP_ACTION = "";

    public  final String OPERATION_NAME = "Add";

    public  final String WSDL_TARGET_NAMESPACE = "";

    public  final String SOAP_ADDRESS = "";
    public CallSoap()
    public String Call(int a,int b)
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
        PropertyInfo pi=new PropertyInfo();
        pi=new PropertyInfo();

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        envelope.dotNet = true;


        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response=null;
  , envelope);
            response = envelope.getResponse();
        catch (Exception exception)
        return response.toString();

step 7 -> open activity_main.xml and copy and past the code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="hello" />

        android:layout_height="wrap_content" >

        <requestFocus />

        android:layout_height="wrap_content" />

        android:text="submit" />


Step 8 -> Open the

And write the code as shown below

import android.R.string;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textDisplay;

    public void onCreate(Bundle savedInstanceState) {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy =
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
        Button b1=(Button)findViewById(;
        //textDisplay =(TextView)findViewById(;
        final  AlertDialog ad=new AlertDialog.Builder(this).create();

        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                CallSoap cs=new CallSoap();
                //soapshowrecord showrec=new soapshowrecord();               
                    EditText ed1=(EditText)findViewById(;
                    EditText ed2=(EditText)findViewById(;
                    int a=Integer.parseInt(ed1.getText().toString());
                    int b=Integer.parseInt(ed2.getText().toString());
                    //EditText ed3=(EditText)findViewById(;
                    //String record=(ed3.getText().toString());
                    ad.setTitle("OUTPUT OF ADD of "+a+" and "+b);
                    //String resp1=showrec.Call1(record);
                    String resp=cs.Call(a, b);
                }catch(Exception ex)
      ; }


Output as shown Below

Monday, 25 February 2013 webservices

Asp.Net Web Services Examples

  Categories : - 

Create RESTful WCF Service API: Step By Step Guide with source code


What is Web Service?

Web Service is an application that is designed to interact directly with other applications over the internet. In simple sense, Web Services are means for interacting with objects over the Internet. The Web serivce consumers are able to invoke method calls on remote objects by using SOAP and HTTP over the Web. WebService is language independent and Web Services communicate by using standard web protocols and data formats, such as

Example of Creating Web Service in .Net

Here are samples codes which I use to create and consume ASP.NET Web Service:
Step 1- Create the ASP.NET Web Service Source File
Open Visual Studio 2010 and create a new web site.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Then, you have to give the name of your service. In this example I am giving it's name "mywebservice". Then Click the ok Button. A screen-shot of these activity is given below.
Step 2- click on the "ok" button; you will see the following window.
Here (in the above figure), you will note that there is predefined method "HelloWorld" which returns the string "Hello World". You can use your own method and can perform various operations. Here I made a simple method which returns the multiplication of two numbers using the code.
Service.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
public int Multiplication(int a,int b)
return (a*b);
Before Debugging the above Web Service see some important term
using System.Web.Services;
This directive allows you to refer to objects in the System.Web.Services namespace without having to fully qualify the request. This statement is optional, but if it is not included, every reference to an object in this namespace must be fully qualified. An example is the next line, which is our class declaration. With the using statement, it looks as follows in C#:
The [WebMethod] attribute The Service class exposes a single method, the public method Multiplication, which takes two integer arguments and returns the multiplication of two number as integer. To expose a method as a part of a web service, you must decorate it with the WebMethod attribute, which tells the compiler to treat it as such. Any method marked with the WebMethod attribute must be defined as public. Class methods exposed as web services follow the same object-oriented rules as any other class, and therefore methods marked private, protected, or internal are not accessible and will return an error if you attempt to expose them using the WebMethod attribute.

In the Solution Explorer you will see

Service.asmx- which contains the following code:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
The page directive WebService is required and the class is the name of the .NET Class to expose the Web Service, each method exposes as Web Service Class Method need to have a declarative attribute statement.
The WebService directive is similar to the Page directive that begins most .aspx pages. For the Multiplication web service to work, you must assign values to two WebService directive attributes: Language and Class.
The required Language attribute lets .NET know which programming language the class has been written in. As you might guess, the acceptable values for the language attribute are currently C#, VB, and JS for JScript.NET.
The Class attribute, also required, tells ASP.NET the name of the class to expose as a web service. Because a web service application can comprise multiple classes, some of which may not be web services, you must tell .NET which class to expose, a step analogous to declaring a Main() method to indicate the entry point of a .NET console application or component. Note that even if your web service contains only one class, setting this attribute is required.
Now back to the our web service.
Step 3- Build Web Service and Run the Web Service for testing by pressing F5 function key.

Copy the url of this web service for further use.
Click on the Mutiplication button to test the web service.

Enter the value of a and b.

By pressing the "Invoke" button a XML file is generated.

Now our web service is ready to use; we just need to create a new web site to consume the web service.
Example of Testing Web Service in .Net.
Step 5- Create a Test Web Site by File > New > Web Site > Web Site

Name the web site, for example here I have choosen the name "Test".
Step 6- Right-click Solution Explorer and choose "Add Web Reference".

Step 7- Paste the url of the web service and click on 'Go' button and then 'Add reference'.

Step 8- Now your web service is ready to use in the Solution Explorer you will see.

Step 9- Go to the design of the Default.aspx page; drag and drop three Textboxes and one button.

step 10-Go to Default.cs page and on the button click event use the following code.
protected void Button1_Click(object sender, EventArgs e)
localhost.Service mys = new localhost.Service(); // you need to create the object of the web service
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
int c = mys.Multiplication(a, b);
TextBox3.Text = c.ToString();
Step 11- After pressing the F5 function key to run the website, you will see:

Enter the number.

Press the show button

Create RESTful WCF Service API: Step By Step Guide with source code



New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result