Click here to Skip to main content
15,886,847 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I create simple C# webservice.and i cant call this webservice from android C# application if any one know plz tell me. i m stuck in this since 1 week.plz help me on this. or if any one know how to get data from sql server in c# android application then no problem give me that answer.thank you so much in advance. :)
Posted
Comments
Sergey Alexandrovich Kryukov 8-May-14 2:44am    
What have you tried so far?
If you are using Xamarin or not, it's still SLR, the platform does not matter much. And there is no such thing as "normal" Web service. You have to be more specific. What's the problem, exactly?
—SA
Sunasara Imdadhusen 8-May-14 3:04am    
Can you please provide code snippet for the same!
Patel Shweta 8-May-14 3:26am    
ya sure
Patel Shweta 8-May-14 3:54am    
my activity.cs file code is:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using webserviceexample.com.cdy.ws;
using System.Net;
using System.IO;
using System.Text;

namespace webserviceexample
{
[Activity(Label = "webserviceexample", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
int count = 1;

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
TextView lable = FindViewById<textview>(Resource.Id.lbltxt);
com.cdy.ws.Androidhelloo dd = new com.cdy.ws.Androidhelloo();
lable.Text = dd.HelloWorld();
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
}


My webservice code is

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

namespace AndroidHello
{
///
/// Summary description for Androidhelloo
///

[WebService(Namespace = "http://127.0.0.1:8012/Androidhelloo.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 Androidhelloo : System.Web.Services.WebService
{
[SoapDocumentMethod(Action = "http://127.0.0.1:8012/Androidhelloo.asmx")]
[WebMethod]
public string HelloWorld()
{

return "Hello World";
}
public string Namespace { get; set; }
}

}

1 solution

my activity.cs file code is

C#
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using webserviceexample.com.cdy.ws;
using System.Net;
using System.IO;
using System.Text;

namespace webserviceexample
{
    [Activity(Label = "webserviceexample", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            TextView lable = FindViewById<textview>(Resource.Id.lbltxt);
           com.cdy.ws.Androidhelloo dd = new com.cdy.ws.Androidhelloo();
          lable.Text = dd.HelloWorld();
            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        }
    }
}</textview>



My webservice code is


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace AndroidHello
    {
    /// <summary>
    /// Summary description for Androidhelloo
    /// </summary>
    [WebService(Namespace = "http://127.0.0.1:8012/Androidhelloo.asmx")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Androidhelloo : System.Web.Services.WebService
        {
        [SoapDocumentMethod(Action = "http://127.0.0.1:8012/Androidhelloo.asmx")]
        [WebMethod]
        public string HelloWorld()
            {

            return "Hello World";
            }
        public string Namespace { get; set; }
        }

    }



Error come---

C#
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18052
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by Microsoft.VSDesigner, Version 4.0.30319.18052.
// 
#pragma warning disable 1591

namespace webserviceexample.com.cdy.ws {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;
    using System.ComponentModel;
    
    
    /// <remarks />
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.17929")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="AndroidhellooSoap", Namespace="http://127.0.0.1:8012/Androidhelloo.asmx")]
    public partial class Androidhelloo : System.Web.Services.Protocols.SoapHttpClientProtocol {
        
        private System.Threading.SendOrPostCallback HelloWorldOperationCompleted;
        
        private bool useDefaultCredentialsSetExplicitly;
        
        /// <remarks />
        public Androidhelloo() {
            this.Url = "http://127.0.0.1:8012/Androidhelloo.asmx";
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }
        
        public new string Url {
            get {
                return base.Url;
            }
            set {
                if ((((this.IsLocalFileSystemWebService(base.Url) == true) 
                            && (this.useDefaultCredentialsSetExplicitly == false)) 
                            && (this.IsLocalFileSystemWebService(value) == false))) {
                    base.UseDefaultCredentials = false;
                }
                base.Url = value;
            }
        }
        
        public new bool UseDefaultCredentials {
            get {
                return base.UseDefaultCredentials;
            }
            set {
                base.UseDefaultCredentials = value;
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }
        
        /// <remarks />
        public event HelloWorldCompletedEventHandler HelloWorldCompleted;
        
        /// <remarks />
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://127.0.0.1:8012/Androidhelloo.asmx", RequestNamespace = "http://127.0.0.1:8012/Androidhelloo.asmx", ResponseNamespace = "http://127.0.0.1:8012/Androidhelloo.asmx", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string HelloWorld() {
           <big> object[] results = this.Invoke("HelloWorld", new object[0]);</big>
            return ((string)(results[0])); //Error Come here in reference.cs file
        }
        
        /// <remarks />
        public void HelloWorldAsync() {
            this.HelloWorldAsync(null);
        }
        
        /// <remarks />
        public void HelloWorldAsync(object userState) {
            if ((this.HelloWorldOperationCompleted == null)) {
                this.HelloWorldOperationCompleted = new System.Threading.SendOrPostCallback(this.OnHelloWorldOperationCompleted);
            }
            this.InvokeAsync("HelloWorld", new object[0], this.HelloWorldOperationCompleted, userState);
        }
        
        private void OnHelloWorldOperationCompleted(object arg) {
            if ((this.HelloWorldCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.HelloWorldCompleted(this, new HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }
        
        /// <remarks />
        public new void CancelAsync(object userState) {
            base.CancelAsync(userState);
        }
        
        private bool IsLocalFileSystemWebService(string url) {
            if (((url == null) 
                        || (url == string.Empty))) {
                return false;
            }
            System.Uri wsUri = new System.Uri(url);
            if (((wsUri.Port >= 1024) 
                        && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {
                return true;
            }
            return false;
        }
    }
    
    /// <remarks />
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.17929")]
    public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e);
    
    /// <remarks />
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.17929")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class HelloWorldCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        internal HelloWorldCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        /// <remarks />
        public string Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((string)(this.results[0]));
            }
        }
    }
}

#pragma warning restore 1591
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900