【问题标题】:Android java object doesn't match with Wcf Service C# objectAndroid java 对象与 Wcf Service C# 对象不匹配
【发布时间】: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>();
.
.
.
}

【问题讨论】:

    标签: java c# android wcf


    【解决方案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; ");
    

    它成功了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多