JSONStringer在Android 2.1中给出null对象(更新)

时间:2011-05-28 22:34:21

标签: android json gson

我通过以JSON格式发送对象来对Web服务进行调用。我使用GSON将我的对象转换为JSONJSONStringer,以创建要在post调用中发送到服务器的键值对。

    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());)获取null异常的原因 谁能告诉我这里有什么问题? 或者有没有办法让键值对在post中进行GSON调用,以便JSONStringer可以删除?

由于 Ashwani

1 个答案:

答案 0 :(得分: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();

需要平衡对象语法 - 有一个开头和一个结尾。