使用webapi服务器端的Android httppost对象参数为null

时间:2013-11-10 23:06:00

标签: c# android json entity-framework asp.net-web-api

我的问题是无法在服务器端正确填充我的对象,它是空的。

这是我的安卓代码:

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();
}

我的网络代码:

 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 Contetial Headers:Content -Length = 267 Content-Type = application / json; charset = utf-8

这是我的服务器端错误:

消息:值不能为空。参数名称:实体堆栈跟踪:位于ShowMe的System.Data.Entity.DbSet`1.Add(TEntity实体)的System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires(布尔条件,String userMessage,String conditionText)。 Controllers.UsuarioController.Post(Usuario usuario)inner:来源:EntityFramework目标:Void需要(Boolean,System.String,System.String)null

我认为问题出在我的json数据中。在我的学习和测试期间,如果我在我的json数据中只传递了两个attributs,那么工作正常并且服务器端的参数被正确填充。使用这样的json数据:{“IMEI”:“353317051196773”,“facebookId”:“444401625902459”}。

任何人都可以帮助我?

0 个答案:

没有答案