Top Ad unit 728 × 90

How to call asp.net web service from android app

How to call asp.net web service from android app


Recently Someone ask us a question about How to call asp.net web service from android app?
A year ago I was working on a android app development project where I had to call asp.net web service from android app to store data in sql server database.
To call asp.net web service from android app basic steps are as following:
1.Create a asp.net web project.
2.Add a web service in asp.net web project.
3.Add a asp.net class file.
4.Create sql server database with required fileds.
5.Create android project in Android Studio.
6.Import KSOAP library in android project.
7.Implement logic in android app.


In my senario,I had to call webservice to store data in remote sql server from android app.

Create a sql table CustomerDetails with following fields:
[CustomerID] int identity colum for auto increment;
[Customer_Name] String,
[Address] String,
[Email] String,
[Mobile_No] bigint for storing only numbers,
[Phone_No] bigint for storing only numbers,
[map1] double to store latitude,
[map2] double to store longitude

You may change fields as per your requirements but you have to change both Web Service file and Class file to get code working .
1.Create a asp.net project in visual studio: File>>New>>Website>>Asp.net Empty Websit.Give a name for your project and then press ok.



2.To add a web service in asp.net project:Add New Item>>Select Web Service>>Press "Add".After adding web service in asp.net project the code file will appear under App_Code folder.Double click on web service file and add the Code:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
///

/// Summary description for adddata
///
[WebService(Namespace = "http://tempuri.org/")]
[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 adddata : System.Web.Services.WebService {

    public adddata () {

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

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }


    [WebMethod]
    public cadd[] insertdata(string customername, string address, string email, string mobile, string phone, string latitude, string longitude)
    {
        List retVal = new List();

//connection string is stored in web.config file
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);




        try
        {

            string sql = "INSERT INTO CustomerDetails (Customer_Name,Address,Email,Mobile_No,Phone_No,map1,map2) VALUES ('" + customername + "','" + address + "','"+email+"','" + mobile + "','" + phone + "','" + latitude + "','" + longitude + "')";
            sql += "select * from customer where customer_Name='" + customername + "' and mobile_No='" + mobile + "' and address='" + address + "'";




            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (dr.Read())
            {
                cadd jb = new cadd(Convert.ToInt32(dr["Customer_id"]), dr["Customer_Name"].ToString(), Convert.ToString(dr["Address"]), dr["Customer_Name"].ToString(), Convert.ToInt64(1), Convert.ToInt64(1), ((double)(0)), ((double)(0)));
                retVal.Add(jb);



            }

        }

        catch
        {
            string res = "Faild!Please Provide correct Data and try again";
            cadd jb = new cadd(Convert.ToInt32(0), Convert.ToString(customername), Convert.ToString(res), Convert.ToString(res),Convert.ToInt64(1), Convert.ToInt64(1), ((double)(0)), ((double)(0)));
            retVal.Add(jb);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }



        return retVal.ToArray();
    }





 
}


3.Now create a class file in asp.net project:Add new Item>>Enter Class Name>>Press Ok.After adding double click on class file and enter the code bellow:



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

///

/// Summary description for cadd
///
public class cadd
{
    private int _CustomerID = 0;
    private string _CustomerName = null;
    private string _Address = null;
    private string _Email = null;
    private Int64 __Mobile = 0;
    private double _Latitude = 0;
    private double _Longitude = 0;
    private Int64 _Phone = 0;

     public int CustomerID
    {
        set { _CustomerID = value; }
        get { return _CustomerID; }
    }


    public string CustomerName
    {
        set { _CustomerName = value; }
        get { return _CustomerName; }
    }

    public string Address
    {
        set { _Address = value; }
        get { return _Address; }
    }
    public string Email
    {
        set { _Email = value; }
        get { return _Email; }
    }
    public Int64 Mobile
    {
        set { __Mobile = value; }
        get { return __Mobile; }
    }


 


    public double Latitude
    {
        set { _Latitude = value; }
        get { return _Latitude; }
    }

    public double Longitude
    {
        set { _Longitude = value; }
        get { return _Longitude; }
    }

    public Int64 Phone
    {
        set { _Phone = value; }
        get { return _Phone; }
    }
public cadd()
{
//
// TODO: Add constructor logic here
//
}
     public cadd(int customerid, string customername, string address,string email, Int64 mobile, Int64 phone, double lat, double longt)
{
        _CustomerID = customerid;
       _CustomerName =customername;
        _Address = address;
        _Email = email;
        __Mobile = mobile;
        _Phone = phone;
        _Latitude = lat;
        _Longitude = longt;
     

}


}

Your asp.net project is complete to store data sent from android app.Now you have to create a new android project to send data.

I will discuss on android part on next post of How to call asp.net web service from android app.
For any support or help comment bellow.



How to call asp.net web service from android app Reviewed by Abdul Moid on October 27, 2015 Rating: 5

Contact Form

Name

Email *

Message *

Powered by Blogger.