我通过以JSON
格式发送对象来对Web服务进行调用。我使用GSON
将我的对象转换为JSON
和JSONStringer
,以创建要在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
答案 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();
需要平衡对象语法 - 有一个开头和一个结尾。