【发布时间】:2017-06-20 15:53:20
【问题描述】:
这是我的课:
public class PTList
{
private String name;
public PTList() { }
public PTList(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
和我的 RestSharp POST 请求:
protected static IRestResponse httpPost(String Uri, Object Data)
{
var client = new RestClient(baseURL);
client.AddDefaultHeader("X-Authentication", AuthenticationManager.getAuthentication());
client.AddDefaultHeader("Content-type", "application/json");
var request = new RestRequest(Uri, Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(Data);
var response = client.Execute(request);
return response;
}
当我使用带有良好 URI 和 PTList 对象的 httpPost 方法时,前面的 API anwser 表明“名称”为空。 我认为我的 PTList 对象在 API 请求中未序列化为有效 JSON,但无法理解发生了什么问题。
【问题讨论】:
-
您可以尝试一些事情,首先通过添加断点或在控制台上显示它来确保您传递的
Data格式有效,其次您可以尝试更简单的类,例如@987654324 @ 具有公共可访问字段名称。 -
AFAIK 序列化将序列化 properties 并且没有属性 => 没有内容,只是像 {} 这样的空对象