JSON对象在wcf rest中发布为null

时间:2013-06-27 04:53:16

标签: c# android json wcf

我是Android的新手,我有一个简单的Android应用程序,它收集信息并保存到db.I想将此信息保存在我的主机中的sql server中。我在aSync任务中发布信息,如下所示:

     
          @Override
       protected JSONObject doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    // POST request to <service>/SaveVehicle
    HttpPost request = new HttpPost(url);
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");

    // Build JSON string
    StringEntity entity;
    try {
        //Gson gson=new Gson();
        //String jstr = gson.toJson(jsonObjSend);
        //Log.d("json", jstr);
        entity = new StringEntity(jsonObjSend.toString(),"UTF-8");
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        entity.setContentType("application/json");

        //InputStream content = entity.getContent();

        request.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 



    // Send request to WCF service
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response;
    try {
        response = httpClient.execute(request);
        Log.d("WebInvoke", "Saving : " +     response.getStatusLine().getStatusCode());
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return null;
}

我的wcf Iservice看起来像这样:

        [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "CustInfo")]
    [OperationContract]
    ResponseCustInfo CustInfo(RequestCustInfo CustInfo);


[Serializable, DataContract(Name = "CustInfo")]
public class RequestCustInfo
{


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

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

    [DataMember(Name = "brands")]
    public List<RequestCustBrands> brands
    {
        get;
        set;
    }

 }
    //service :
    public ResponseCustInfo CustInfo(RequestCustInfo CustInfo)
    {
        ResponseCustInfo res = new ResponseCustInfo();
        try
        {
            Android.Sync.CustInfo CustDeserializedObj = new Android.Sync.CustInfo();
            if (CustInfo != null)
            {
               CustDeserializedObj.StoreName = CustInfo.StoreName;
               CustDeserializedObj.City = CustInfo.City;
               CustDeserializedObj.brands = CustInfo.brands;
            }
            //save object to sql...

我的json字符串是这样的:

{"CustInfo":[{"StoreName":"test","City":"تهران"},[{"CustomerRef":"25","BrandRef":"272","GoodsRef":"21"},{"CustomerRef":"25","BrandRef":"272","GoodsRef":"21"}]]}

我的问题是当我调试服务时“CustInfo”对象为空。

我提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

问题是在我的项目中使用JSON对象的方式我使用了JSONStringer并且它运行良好。

相关问题