-->

KSOAP2了java.lang.RuntimeException:无法序列(KSOAP2 java

2019-09-21 02:42发布

我试图用一个方法从.NET Web服务。

Web服务背后的代码有一个“/”在命名空间的结尾

[WebService(Namespace = "http://www.mynamespace.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

以下是.NET方法调用

POST /TelematicsWebService/Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.mynamespace.com/GetDevicesByBranch"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetDevicesByBranch xmlns="http://www.mynamespace.com/">
      <branchNumber>int</branchNumber>
    </GetDevicesByBranch>
  </soap:Body>
</soap:Envelope>

现在代码调用该方法看起来是这样的

    @Override
    protected SoapObject doInBackground(Integer... branchNumber) {

        final String NAMESPACE = "http://www.mynamespace.com/";
        final String METHOD_NAME = "GetDevicesByBranch";
        final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
        final String URL = "http://10.0.2.2/TelematicsWebService/Service.asmx";

        SoapObject response = null;

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo inputArgs = new PropertyInfo();
            inputArgs.setName("branchNumber");
            inputArgs.setValue(branchNumber);
            inputArgs.setType(Integer.class);
            request.addProperty(inputArgs);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,
                    25000);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            response = (SoapObject) envelope.getResponse();
        } catch (Exception exception) {
            response = null;
        }

        return response;
    }

朝向端部的线, androidHttpTransport.call(SOAP_ACTION, envelope); ,抛出一个异常: java.lang.RuntimeException: Cannot serialize: [Ljava.lang.Integer;@412e0cf0

我不能找到一个解决这个问题,有什么建议?

Answer 1:

我想到了...

这是内部的的AsyncTask <>(),如果它不是已与该覆盖doInBackground()方法明显。 输入参数是一个Integer...类型。

protected SoapObject doInBackground(Integer... branchNumber)

我不知道省略号做究竟是什么,但我知道它使参数数组。

我经过一个整数阵列到请求对象,而不是一个特定的数组元素。

更改

request.addProperty("branchNumber", branchNumber);

request.addProperty("branchNumber", branchNumber[0]);

固定的问题。

谢谢@保罗 - 扬和@beginner! 我很欣赏你的答案!



Answer 2:

代替

PropertyInfo inputArgs = new PropertyInfo();
inputArgs.setName("branchNumber");
inputArgs.setValue(branchNumber);
inputArgs.setType(Integer.class);
request.addProperty(inputArgs);

尝试这个:

request.addProperty("branchNumber", branchNumber);


Answer 3:

对于那些谁想要添加对象的列表作为Web服务的参数:

List<Object> vect = new Vector<Object>();
vect.add("Element1");
vect.add("Element2");
vect.add("Element3");

PropertyInfo tabProp =new PropertyInfo();
tabProp.setName("LIST_PARAM");          
tabProp.setValue(vect);

request.addProperty(tabProp );

当然,有没有需要注意的是异步任务必须实现的方法:

protected String doInBackground(List<Object>... params){
....
....
} 


文章来源: KSOAP2 java.lang.RuntimeException: Cannot serialize