我使用json将对象从Android应用程序发送到Web Api应用程序。 当我尝试发送一个包含布尔值的对象时,即使它在json中为true,它的值也会被反序列化为false! 这是一个例子:
public class myObject{
public boolean value1;
public String value2;
}
我将此方法用于服务器的对象:
public String PostObject(String url, Object obj) throws ParseException, IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create().toJson(obj));
httpPost.setEntity(stringEntity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = client.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String resp = EntityUtils.toString(httpEntity);
return resp;
}
最后,我将我的对象发送到服务器:
myObject obj = new myObject();
obj.value1 = true;
obj.value2 = "testing";
JSONHttpClient jsonHttpClient = new JSONHttpClient();
String response = jsonHttpClient.PostObject(myURL, obj);
这是我服务器端的代码:
[AcceptVerbs("POST")]
public string test(myObject obj)
{
//obj.value1 = false !!!
}