Search code examples
androidweb-servicesksoap2

ksoap2 cannot serialize exception for byte[]


I'm trying to consume an Asp.Net web service. I'm using ksoap2 library. But I'm getting "java.lang.RuntimeException: Cannot serialize: [B@41eb9c40" exception. I'm trying to send a file via web service. I think the problem is caused by byte array. Here is my code:

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    String savePath = MainActivity.this.getFilesDir().getPath()
            + "/asd.txt";
    RandomAccessFile file = new RandomAccessFile(savePath, "r");
    byte[] fileByte = new byte[(int) file.length()];
    file.readFully(fileByte);
    file.close();

    Request.addProperty("file", fileByte);
    Request.addProperty("fileName", "fileNameDir.txt");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);
    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
            URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject) envelope.getResponse();
        System.out.println(response.getProperty(1).toString());
    } catch (Exception e) {
        System.out.println("exc: " + e);
    }

Solution

  • I fixed it. I added this line of code and it worked:

        MarshalBase64 marshal = new MarshalBase64();
        marshal.register(envelope);