【发布时间】:2013-05-12 08:27:14
【问题描述】:
我有一个书面的 Web 服务,以及我编写的用于调用其中一个方法的 java 方法。
该服务返回一个JSON 字符串。
由于 NDA 协议,我无法在此处发布任何服务代码,但这也无关紧要,因为从服务本身激活方法时,它会给出正确的结果。
基本上,java 代码会跳过数组中的第一个单元格。
这是原始结果(直接来自服务):
{"message":"Success","success":"1","Table" : [{"priceline" : "Price 1","percaret_price" : "1430.0000","total" : "757.9000","discount" : "-45.00"},{"priceline" : "Price 2","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "Price 3","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "Cash","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "MSRP","percaret_price" : "","total" : "","discount" : ""}]}
这是java代码的结果:
{"message":"Success","success":"1","Table" : [{"priceline" : "Price 2","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "Price 3","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "Cash","percaret_price" : "","total" : "","discount" : ""},{"priceline" : "MSRP","percaret_price" : "","total" : "","discount" : ""}]}
*为了便于阅读,您可以使用 json 查看器: http://jsonviewer.stack.hu/
这是我的 java 代码(所有变量都是正确的。如果不正确,则不返回任何结果):
SoapObject request = new SoapObject(NAMESPACE, COST_INFORMATION_NAME);
// Use this to add parameters
request.addProperty("user_id", login.getUser_id());
request.addProperty("company_id", login.getCompany_id());
request.addProperty("inventoryID", inventoryId);
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(COST_INFORMATION_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
String res = result.getPropertyAsString(0);
//************************************************************
//Everything else isn't relevant, the res variable contains the result I put above the code.
//************************************************************
感谢您的帮助!
【问题讨论】:
-
您是否使用 JSON 进行 SOAP 响应?
-
当然。结果为 JSON 格式。
-
您正在尝试将 JSON 转换为 Java 对象,并且在此过程中数组的第一行被跳过?
-
没有。看起来从一开始它就跳过了第一个。这与我使用 JSON 的事实无关。我从服务中得到的字符串是错误的