KSOAP2 java.lang.RuntimeException: Cannot serialize

好久不见. 提交于 2019-12-12 15:34:16

问题


I am trying to use a method from a .net web service.

The code behind for the web service has a '/' at the end of the namespace

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

Here is the .net method call

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>

Now the code calling the method looks like this

    @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;
    }

The line towards the end, androidHttpTransport.call(SOAP_ACTION, envelope);, throws an exception: java.lang.RuntimeException: Cannot serialize: [Ljava.lang.Integer;@412e0cf0

I can't find a solution to this issue, any suggestions?


回答1:


I figured it out...

This is inside an AsyncTask<>() if it wasn't already obvious with the Override doInBackground() method. The input parameter is a Integer... type.

protected SoapObject doInBackground(Integer... branchNumber)

I don't know what the ellipsis does exactly, but I know it makes the parameter an array.

I was passing an integer array into the request object rather than a specific array element.

changing from

request.addProperty("branchNumber", branchNumber);

to

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

fixed the issue.

Thank you @Paul-Jan and @beginner! I appreciate your answers!




回答2:


Instead of

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

try this:

request.addProperty("branchNumber", branchNumber);



回答3:


For those who want to add a list of Object as a parameter of web service :

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 );

Of course there is no need to note that the Async Task must implements the method :

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


来源:https://stackoverflow.com/questions/11907434/ksoap2-java-lang-runtimeexception-cannot-serialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!