Pages

Ads 468x60px

For New Update Use this Link http://dotnethubs.blogspot.in/ Offer for you

.

Wednesday 17 July 2013

How to call asp.net web service from android application

create simple drop down menu using jquery
Introduction : -

 In this  article i am explaining how to call the asp.net  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

File->new->Project
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 = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

  

    [WebMethod]
    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
ksoap2-android-assembly-2.5.5-jar-with-dependencies.jar

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 CallSoap.java]

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 = "http://tempuri.org/Add";

    public  final String OPERATION_NAME = "Add";

    public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    public  final String SOAP_ADDRESS = "http://179.45.33.245/webservice/WebService.asmx";
    public CallSoap()
    {
    }
    public String Call(int a,int b)
    {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
        PropertyInfo pi=new PropertyInfo();
        pi.setName("a");
        pi.setValue(a);
        pi.setType(Integer.class);
        request.addProperty(pi);
        pi=new PropertyInfo();
        pi.setName("b");
        pi.setValue(b);
        pi.setType(Integer.class);
        request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response=null;
        try
        {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (Exception exception)
        {
            response=exception.toString();
        }
        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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="230dp"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="232dp"
        android:layout_height="wrap_content" />
   
   
  
   

    <Button
        android:id="@+id/button1"
        android:layout_width="229dp"
        android:layout_height="wrap_content"
        android:text="submit" />

   

</LinearLayout>
 
Step 8 -> Open the MainActivity.java

And write the code as shown below

import android.R.string;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy =
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        //textDisplay =(TextView)findViewById(R.id.text);
        //textDisplay.setText("heloo");
        final  AlertDialog ad=new AlertDialog.Builder(this).create();

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

}

Output as shown Below



1 comment:

 

..




New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result