0

The following code samples are from my aspx web service.

I would like to know how it should be changed so that i could return it as an object. All the following codes are on the web service and i am trying to call the object from Android.

So i would just like to know what changes i have to make in order to be able to pass the object.

Any help would be greatly appreciated.

[WebMethod]
public object SomeMethod(Vehicle obj)
{
    return obj;
}

[WebMethod]
public void simpleCase()
{
    Vehicle obj = new Vehicle();
    obj.VehicleID = "KL-9876";
    obj.VehicleType = "Nissan";
    obj.VehicleOwner = "Sanjiva";
}

public class Vehicle
{
    public string VehicleID { get; set; }
    public string VehicleType { get; set; }
    public string VehicleOwner { get; set; }
}
4

2 回答 2

1

如果我理解正确,您想从您的 Android 应用程序创建一个 Web 请求并在您的请求后接收该特定数据,这是 json 和 rest wcf Web 服务的经典案例。

这里有一个如何启动服务器端的链接:http: //msdn.microsoft.com/en-us/library/dd203052.aspx

这是一个链接,向您展示如何在客户端使用它: http ://www.vogella.com/articles/AndroidJSON/article.html

我希望这或多或少是您正在寻找的。

于 2012-12-13T09:19:54.850 回答
0
[DataContract]
public class Salesman
{

    [DataMember(Name = "a")]
    public virtual string Id { get; set; }

    [DataMember(Name = "b")]
    public virtual int RoleId { get; set; }

    [DataMember(Name = "c")]
    public virtual string Name { get; set; }

    [DataMember(Name = "d")]
    public virtual string Address { get; set; }

    [DataMember(Name = "e")]
    public virtual string Phone { get; set; } 
} // "a","b","c","d"... are the values with which you'll identify the object properties - client side - when json parsing.




 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class SalesmanService 
    {

        [OperationContract]
        [WebGet(UriTemplate = "/get/{id}", 
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public Salesman Get(string id)
        {
          //return your salesman
        }
    }

不要忘记 global.asax 中的这一点:

RouteTable.Routes.Add(new ServiceRoute("salesman", new WebServiceHostFactory(), typeof(SalesmanService)));

基本上你应该可以拨打这样的电话:例如“..localhost/salesman/50”

这只是一个小示例,您必须亲身体验 wcf 和 json 以获得更广泛的理解。

于 2012-12-13T09:37:37.680 回答