我的问题是无法在服务器端正确接收我的对象,它为空。
这是我的安卓代码:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(getputServiceUrl());
Gson holder = new Gson();
StringEntity se = null;
try {
se = new StringEntity(s);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json; charset=utf-8");
try {
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse response = httpclient.execute(httpost, responseHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的 webapi 代码:
public HttpResponseMessage Post(Usuario usuario)
{
try
{
context.Usuarios.Add(usuario);
context.SaveChanges();
var response = Request.CreateResponse(HttpStatusCode.Created, usuario);
response.Headers.Location = new Uri(Request.RequestUri, string.Format("usuario/{0}", usuario.Id));
return response;
}
catch (Exception ex)
{
var contextLog = new showaboutmeDBEntities();
contextLog.Logs.Add(new Log()
{
error = "Message: " + ex.Message + "\n\n\n\nStack Trace:" + ex.StackTrace +
"\n\n\ninner: " + ex.InnerException + "\n\n\nSource:" + ex.Source + "\n\n\nTarget: " + ex.TargetSite +
"\n\n\n\n\n\n\n\n\n\n\n\n" + JsonConvert.SerializeObject(usuario)
});
contextLog.SaveChanges();
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
我做错了什么?
我对此进行了很多搜索,但找不到任何方法。
在我的服务器端,我收到了 null 对象。
这是我的 json 数据:
[{"IMEI":"353317051196773","facebookId":"444401625902459","facebookNome":"Teste Teste","facebookSexo":"male","facebookUserName":"teste.teste.3","googleEmail" :"teste@gmail.com","id":"80e8aa9d-8c39-43d9-a3c7-a851661c87f3"}]
这里是httprequest标头:
标头: Connection = Keep-Alive Accept = application/json Host = showaboutme.azurewebsites.net Max-Forwards = 10 User-Agent = Apache-HttpClient/UNAVAILABLE X-LiveUpgrade = 1 X-MS-REQUEST-ID = 856f5220-9e84- 4480-b2a3-26cf563ba4b7 X-Original-URL = /api/usuario X-Forwarded-For = 189.83.1.142:64031 X-ARR-LOG-ID = e19abeb1-54cd-45c5-8dbb-c034588d0098 内容标头:内容长度 =第267章 字符集=utf-8
这是我的服务器端错误:
消息:值不能为空。参数名称:实体堆栈跟踪:在 System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires(Boolean condition, String userMessage, String conditionText) at System.Data.Entity.DbSet`1.Add(TEntity entity) at ShowMe。 Controllers.UsuarioController.Post(Usuario usuario) 内部:来源:EntityFramework 目标:Void Requires(Boolean, System.String, System.String) null
我认为问题出在我的 json 数据中。在我的学习和测试过程中,如果我在我的 json 数据中只传递了两个属性,那么工作正常并且服务器端的参数被正确填充。使用这样的 json 数据:{"IMEI":"353317051196773","facebookId":"444401625902459"}。
任何人都可以帮助我吗?