Android java 对象与 Wcf Service C# 对象不匹配

Posted

技术标签:

【中文标题】Android java 对象与 Wcf Service C# 对象不匹配【英文标题】:Android java object doesn't match with Wcf Service C# object 【发布时间】:2018-12-09 16:09:12 【问题描述】:

我有一个使用 wcf 服务的 android 项目。我在 java 端的 Select 类型中发送一个对象作为参数。但它与它的 C# 类不匹配。在 wcf 端总是null

这是我的选择功能:

Select select = new Select();
select.setOrderBy(Select.OrderBy.Desc);
select.setOrderColumn("CategoryName");
select.setTop(3);

Where where = new Where();
where.setColumn("ID");
where.setValue("3");
where.setOperators(Where.Operators.GreaterEqual);

jsonObject.put("select", select);
jsonObject.put("where", where);

new TDJson(jsonObject, new TDJsonListener() 
@Override
public void successCallBack(String jsonResult) 
    try 
        JSONObject json = new JSONObject(jsonResult);
        .
        .
        .
     catch (Exception e) 
        e.printStackTrace();
    


@Override
public void errorCallBack() 

, true).execute("http://address/Service/Select");

这是我的 TDJson 代码:

try 
HttpParams httpParams = new BasicHttpParams();
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(params[0]);

if (jsonObj != null) 
    if (!jsonObj.toString().equals("")) 
        httpPost.setEntity(new StringEntity(jsonObj.toString(), "UTF-8"));
    


httpPost.setHeader("Accept", "application/jsonStr");
httpPost.setHeader("Content-type", "application/jsonStr; charset=utf-8; ");
httpPost.setHeader("Cache-Control", "max-age=600");

HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response = httpClient.execute(httpPost);

if (response.getStatusLine().getStatusCode() == HttpsURLConnection.HTTP_OK) 
    jsonResult = EntityUtils.toString(response.getEntity());

 else 
    jsonResult = "error";

 catch (IOException e) 
jsonResult = "error";


return jsonResult;

这是我在 java 中的 Select 类:

public class Select 
public Integer top;
public String orderColumn;
public OrderBy orderBy;
public String[] columns;

public Integer getTop() 
    return top;

public String getOrderColumn() 
    return orderColumn;

public OrderBy getOrderBy() 
    return orderBy;

public String[] getColumns() 
    return columns;


public void setTop(Integer top) 
    this.top = top;

public void setOrderColumn(String orderColumn) 
    this.orderColumn = orderColumn;

public void setOrderBy(OrderBy orderBy) 
    this.orderBy = orderBy;

public void setColumns(String[] columns) 
    this.columns = columns;


public enum OrderBy 
    Asc,
    Desc


这是我在 C# 中的 Select 类:

 [DataContract]
 public class Select
 
 public Select()
 
    top = -1;
    orderColumn = null;
    orderBy = OrderBy.Asc;
    columns = null;
 

[DataMember]
public Int32 top  get; set; 
[DataMember]
public String orderColumn  get; set; 
[DataMember]
public OrderBy orderBy  get; set; 
[DataMember]
public String[] columns  get; set; 

[DataContract]
public enum OrderBy

    [EnumMember]
    Asc,
    [EnumMember]
    Desc


这也是我的 wcf 代码(这里选择对象总是null):

public List<CategoryData> Select(Select select, Where where)

Table<Category> table = new Table<Category>();
.
.
.

【问题讨论】:

【参考方案1】:

我只是改变了这些行

httpPost.setHeader("Accept", "application/jsonStr");
httpPost.setHeader("Content-type", "application/jsonStr; charset=utf-8; ");

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=utf-8; ");

它成功了:)

【讨论】:

以上是关于Android java 对象与 Wcf Service C# 对象不匹配的主要内容,如果未能解决你的问题,请参考以下文章

Android访问WCF服务

使用具有复杂对象的 KSoap2 调用 WCF 服务。 WCF 接收空值

WCF引用服务和直接用接口的区别

流式处理 WCF 数据服务响应

如何在IIS中配置WCF

为啥在 Worker 角色中托管 WCF 服务