使用SoapObject作为参数调用WCF Web服务时出现'无法序列化'错误。在测试其他服务时,将SoapObject作为字符串发送到参数时工作正常。
我现在有点迷茫。那里有任何安卓杀手。
CODE:
final String URL ="http://192.168.1.8:9608/WcfServiceShreeHari/Service1.svc";
final String NAMESPACE = "http://tempuri.org/";
final String METHOD = "GetName";
final String ACTION = "http://tempuri.org/IService1/GetName";
String resp = "";
try {
JSONArray array=null;
JSONObject object=null;
JSONObject mainObject = null;
object=new JSONObject();
mainObject=new JSONObject();
array=new JSONArray();
mainObject.put("UserName", "ss");
mainObject.put("Token", "tokxd43cxen");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("request", mainObject);
envelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call( ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
resp = e.getMessage();
} catch (XmlPullParserException e) {
e.printStackTrace();
resp = e.getMessage();
}
if (envelope.bodyIn != null) {
//SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
// SoapPrimitive resultSOAP = (SoapPrimitive) envelope.getResponse();
resp = envelope.getResponse().toString();
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
ERROR:
06-18 19:11:01.839: W/System.err(7682): java.lang.RuntimeException: Cannot serialize: {"UserName":"ss","Token":"tokxd43cxen"}
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:708)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:692)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:661)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:645)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:702)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:618)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:198)
06-18 19:11:01.844: W/System.err(7682): at org.ksoap2.transport.Transport.createRequestData(Transport.java:107)
06-18 19:11:01.849: W/System.err(7682): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
06-18 19:11:01.849: W/System.err(7682): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
06-18 19:11:01.849: W/System.err(7682): at com.example.srihari.LogIn$webService.webServiceCall(LogIn.java:903)
06-18 19:11:01.849: W/System.err(7682): at com.example.srihari.LogIn$webService.doInBackground(LogIn.java:398)
06-18 19:11:01.849: W/System.err(7682): at com.example.srihari.LogIn$webService.doInBackground(LogIn.java:1)
06-18 19:11:01.849: W/System.err(7682): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 19:11:01.849: W/System.err(7682): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-18 19:11:01.849: W/System.err(7682): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-18 19:11:01.849: W/System.err(7682): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-18 19:11:01.854: W/System.err(7682): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-18 19:11:01.854: W/System.err(7682): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-18 19:11:01.854: W/System.err(7682): at java.lang.Thread.run(Thread.java:856)
WEB方法:
public string ImportOrders(Request request)
{
var inputObj = request.OrderList;
long UserID = GetUserID(request.UserName, request.Token);
if (UserID == 0)
{
service_error.longMessage = "Error code:100. Validation Failed.Please send proper credentials.";
return CreateResponseXML2("Error", service_error.longMessage);
}
CommonUtilities.UserId = UserID;
CommonUtilities.OrderTotal = inputObj.Count.ToString();
CommonUtilities.OrderSuccess = "";
CommonUtilities.OrderFailure = "";
CommonUtilities.OrderUpdated = "";
CommonUtilities.OrderAdded = "";
CommonUtilities.AddedCustomer = "";
#region set imported orders
foreach (var inputOrder in inputObj)
{
if (string.IsNullOrEmpty(inputOrder.OrderUniqueId) || string.IsNullOrEmpty(inputOrder.TabID))
{
CommonUtilities.OrderErr(inputOrder.OrderUniqueId, "OrderUniqueId or TabID Not Exists");
continue;
}
if (inputOrder.ItemList.Count == 0)
{
CommonUtilities.OrderErr(inputOrder.OrderUniqueId, "ItemList Not provided");
continue;
}
int ErrCount = 0; string ErrProdusts = ""; int itemno = 0; string ProductAttr = ""; int ProductAttrErrCount = 0;
string AttrName = ""; string ErrProdustsAll = "";
using (Data.ShreeHariWebEntities ent = new Data.ShreeHariWebEntities())
{
foreach (var item in inputOrder.ItemList)
{
itemno++;
ProductAttr = "";
ProductAttrErrCount = 0;
if (string.IsNullOrEmpty(item.ModelNumber) || item.Category <=0 || string.IsNullOrEmpty(item.Size) || item.UnitPrice <= 0 || item.Quantity <= 0)
{
if (string.IsNullOrEmpty(item.ModelNumber))
{
AttrName = "ModelNumber";
ProductAttr += string.IsNullOrEmpty(ProductAttr) ? AttrName : ", " + AttrName;
ErrCount++;
ProductAttrErrCount++;
}
if (item.Category <= 0)
{
AttrName = "Catagory";
ProductAttr += string.IsNullOrEmpty(ProductAttr) ? AttrName : ", " + AttrName;
ErrCount++;
ProductAttrErrCount++;
}
if (item.UnitPrice <= 0)
{
AttrName = "UnitPrice";
ProductAttr += string.IsNullOrEmpty(ProductAttr) ? AttrName : ", " + AttrName;
ErrCount++;
ProductAttrErrCount++;
}
if (item.Quantity <=0)
{
AttrName = "Qunatity";
ProductAttr += string.IsNullOrEmpty(ProductAttr) ? AttrName : ", " + AttrName;
ErrCount++;
ProductAttrErrCount++;
}
if (string.IsNullOrEmpty(item.Size))
{
AttrName = "Size";
ProductAttr += string.IsNullOrEmpty(ProductAttr) ? AttrName : ", " + AttrName;
ErrCount++;
ProductAttrErrCount++;
}
if (ProductAttrErrCount != 0)
ErrProdusts = "Missing Attributes " + ProductAttr;
}
else
{
var prod = (from p in ent.Products
where p.ModelNumber == item.ModelNumber && p.Size == item.Size
&& p.Category == item.Category && p.Color == item.Color
select p
).FirstOrDefault();
if (prod == null)
{
AttrName = "Not Exists";
ErrCount++;
ErrProdusts = AttrName;
ProductAttrErrCount++;
}
}
if (ProductAttrErrCount != 0)
ErrProdustsAll += " ##Item-" + itemno.ToString() + " " + ErrProdusts;
}
}
if (ErrCount != 0)
{
CommonUtilities.OrderErr(inputOrder.OrderUniqueId, ErrProdustsAll);
continue;
}
CommonUtilities.OrderValidate = "";
OrderData_Validation(inputOrder);
if (!string.IsNullOrEmpty(CommonUtilities.OrderValidate))
{
CommonUtilities.OrderErr(inputOrder.OrderUniqueId,service_error.longMessage);
continue;
}
if (inputOrder.OrderTotal == 0)
{
decimal orderTotal = 0;
foreach (var item in inputOrder.ItemList)
{
orderTotal += item.UnitPrice * (decimal)item.Quantity;
}
inputOrder.OrderTotal = orderTotal + inputOrder.ShippingPaid + inputOrder.TaxAmount;
}
Order op = new Order();
using (Data.ShreeHariWebEntities ent1 = new Data.ShreeHariWebEntities())
{
op = (from odr in ent1.Orders
where odr.OrderNumber == inputOrder.OrderNumber
&& odr.OrderUniqueId == inputOrder.OrderUniqueId
select odr).FirstOrDefault();
}
if (op == null)
{
oh.AddOrder(inputOrder, new Result());
}
else
{
Result r = new Result();
inputOrder.OrderID = op.OrderID;
inputOrder.StoreID = op.StoreID;
bool UpdateSuccessfull = oh.UpdateManualOrder(inputOrder,r);
if (UpdateSuccessfull)
{
CommonUtilities.OrderUp(inputOrder.OrderUniqueId);
}
else
{
CommonUtilities.OrderErr(inputOrder.OrderUniqueId,"<![CDATA["+r.ErrorMessage+"]]>");
}
}
}
#endregion
string msg = "<TotalOrder>" + CommonUtilities.OrderTotal + "</TotalOrder>";
msg = msg + "<FailedOrders>" + CommonUtilities.OrderFailure + "</FailedOrders>";
msg = msg + "<AddedOrder>" + CommonUtilities.OrderAdded + "</AddedOrder>";
msg = msg + "<UpdatedOrder>" + CommonUtilities.OrderUpdated + "</UpdatedOrder>";
msg = msg + "<Customers>" + CommonUtilities.AddedCustomer + "</Customers>";
return CreateResponseXML2("Success", msg);
}
答案 0 :(得分:1)
您可以使用此
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("request", mainObject.toString());
envelope.setOutputSoapObject(request);
或者,使用此
// Property Name
PropertyInfo piName = new PropertyInfo();
piName.setName("sName");
piName.setValue("Value Here");
piName.setType(JSONObject.class);
request.addProperty(piName);
答案 1 :(得分:0)
试试这种方式,
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("UserName", "ss");
request.addProperty("Token", "tokxd43cxen");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.implicitTypes = false;
envelope.dotNet = true;
可能会对你有帮助。