开发者

JSON request for WCF objects array parameter

I'm asking this question after searching the web for an answer for 2 days.

OK, here's the thing :

In the server side i have a WCF webservice, defined like this:

namespace HelloRest
{
[System.ServiceModel.ServiceContract]
public interface IHelloRest
  {
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "SaveVehicle",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    string SaveVehicle(clVehicles vehicles);
  }
}

namespace HelloRest
{
  public class HelloRestService : IHelloRest
{
   public string SaveVehicle(clVehicles vehicles)
    {
    ...
    }
}
}


namespace WebApplication1
{

    [Serializable]
    [DataContract]
    public class clVehicles
    {
        [DataMember]
        public List<Vehicle> Vehicles { get; set; }
    }
}


namespace WebApplication1
{

    [Serializable]
    [DataContract]
    public class Vehicle
    {

        [DataMember(Name = "year")]
        public int Year
        {
            get;
            set;
        }

        [DataMember(Name = "plate")]
        public string Plate
        {
            get;
            set;
        }

        [DataMember(Name = "make")]
        public string Make
        {
            get;
            set;
        }

        [DataMember(Name = "model")]
        public string Model
        {
            get;
            set;
        }
    }    
}
开发者_如何学Go

And Im trying to consume this webservice with Android, this way:

HttpPost request = new HttpPost(Consts.URL + "/SaveVehicle");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

JSONStringer v = new JSONStringer();
v.object();
v.key("Vehicles");
v.object();
v.key("plate").value(plate);
v.key("make").value(make);
v.key("model").value(model);
v.key("year").value(Integer.parseInt(year.toString()));
v.endObject();
v.endObject();
JSONArray arr = new JSONArray();
arr.put(v);

JSONStringer vehicle = new JSONStringer();
vehicle.object();
vehicle.key("vehicles").value(arr);
vehicle.endObject();

StringEntity entity = new StringEntity(vehicle.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

The problem :

When I'm launching the client, the request is forwarded to the server (I have a breakpoint inside the "SaveVehicle" function) properly.

The problem is with the function's parameter : "vehicles" - this object (suppose to) contains only one member - "Vehicles", which is of type : List. No matter what I'm sending from the client side, I'm getting null value for this inner container ("Vehicles").

I've tried almost everything! I'm sure the problem is somewhere in the JSONStringer area...


Found an answer: Define your classes as follow:

namespace WebApplication1
{
 [KnownType(typeof(Vehicle))]
 [DataContract]
 public class Vehicle
 {
 }
}


namespace WebApplication1
{
 [CollectionDataContract]
 [KnownType(typeof(Vehicle))]
 public class clVehicles: List<Vehicle>
 {      
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜