2011-05-28 20 views
0

我正在通过发送JSON格式的对象向Web服务发送后调用。我正在使用GSON将我的对象转换为JSONJSONStringer以创建键值对,以便在post调用中发送到服务器。JSONStringer在Android 2.1中给出null对象(更新)

JSONStringer billString = new JSONStringer().object() 
       .key("details").value(new JSONObject(json)); 
    CustomHttpRequest request = new CustomHttpRequest(WebserviceBaseAddress); 
     request.AddHeader("Accept", "application/json"); 
     request.AddHeader("Content-type", "application/json"); 
     request.AddParam("entity", billString.toString()); 
     request.GetRequest(RequestMethod.POST); 

这行代码在Android 2.2中正常工作,但2.1,billString为null。这就是为什么我得到空例外(request.AddParam("entity", billString.toString());) 有人可以告诉我这里有什么问题吗? 还是有什么办法使键值对post拨打GSON,这样JSONStringer可以被删除?

感谢 Ashwani

+0

您是否能够发布更完整的代码示例? “它给零”是什么意思?对值方法的最终调用是否返回空引用?你是否从该行代码中获得了NullPointerException? json输入的价值是什么? – 2011-05-29 01:13:48

+0

这里的billString在Android 2.1中为null,但在Android 2.2中,它是一个JSON字符串,其细节为键和json对象作为值。 – 2011-05-29 06:29:41

+0

你能否回答我问的其他问题? – 2011-05-29 06:31:14

回答

1

变化

JSONStringer billString = new JSONStringer().object() 
       .key("details").value(new JSONObject(json)); 

String billString = new JSONStringer().object() 
       .key("details").value(new JSONObject(json)).endObject().toString(); 

的对象语法必须平衡 - 有一个开始和结束。

+0

谢谢布鲁斯,它工作:) – 2011-05-30 07:54:19