RunTimeException:cannot serialize: Error to send byte array from android to java application through ksoap

匆匆过客 提交于 2019-12-10 10:55:33

问题


I have to Send Byte Array to my java application From Android device.I m using ksoap2 for that in android and i have created web service in java with axis2 to receive that byte array and create file at server for further processing.

Let me Give you Full explanation.i am Recording a wave file in android device and then that wave file need to be send at java application which is on server.i had created one web service in java application with axis2 name as "get_wave_byte" which converts the incoming byte array in to a wave file again and store it in server.and from android device i am sending wave file as byte array and also the storage path in the argument of get_wave_byte().

So for that I have created wave file in android and after that i have created one method which converts that wave file in to byte array.the method that converts the wave file in byte array is as follows...

public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

so basically now i am sending those byte array to java application with the following code...

 File source_for_byte=new File(outFilename);//Here is my wave file 

//creating temporary byte array to store that files in byte array

 byte[] temp = new byte[(int) source_for_byte.length()];

//then calling my method as i above stated that converts the file in byte array

temp=getBytesFromFile(source_for_byte);

From here i m using ksoap2 to call java application method in which those array byte as an argument and also string as file path to store at server with the get_wav_byte() method

 String NAMESPACE = "http://test.com";
                String SOAP_ACTION = NAMESPACE + METHOD_NAME;
                // NAMESPACE + method name
                final String URL = "http://192.168.3.106:8080/axis2/services/speechmain?wsdl";

                    METHOD_NAME = "get_wav_byte";
                    try {
                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                        request.addProperty("wavbite",temp);

                        request.addProperty("path", "D:\\sound\\latest_recognizer.wav");
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        Object result = envelope.getResponse();
                        ((TextView) findViewById(R.id.gettext1)).setText("NUMBER IS :->   "
                                 + result.toString());

                    } catch (Exception E) {
                        E.printStackTrace();
                        ((TextView) findViewById(R.id.gettext1)).setText("ERROR:"
                                + E.getClass().getName() + ":" + E.getMessage());
                    }

And my java Application method is that i calling from android is as follows...

 public int get_wav_byte(byte[] wavbite,String path){

        try
            {
                File dstFile = new File(path);
                FileOutputStream out = new FileOutputStream(dstFile);
                    out.write(wavbite, 0, wavbite.length);
                out.close();               
            }
            catch (IOException e)
            {
              System.out.println("IOException : " + e);
            }

         return 0;
         }

and my Problem is When I run my android application to call this web service its giving me following error

 java.lang.runtimeexception:cannot serialize:[B@40781280]

One thing More i want to tell that i am calling same Java Method From My .net application with the following code.

static void Main(string[] args)
        {
            byte[] byteArrayFile = File.ReadAllBytes("D:/Projects/ConsoleApplication1/ConsoleApplication1/1347452692320.wav");

            String mypath="D:\\sound\\bhavik_latest_copy_recorded.wav";


            short[] shortwave=ConvertBytesToShorts(byteArrayFile);
          Speech_service.speechmainPortTypeClient obj = new Speech_service.speechmainPortTypeClient();

            obj.get_wav_byte(byteArrayFile,mypath);
}

And Simply Its Working like Champ and storing my wave file easily

So what is the Problem in my android Code that it gives the error.

can u please tell me whats going wrong?

thank you in advance.


回答1:


Ok.I had Solved my error with the

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            new MarshalBase64().register(envelope); // serialization



回答2:


    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    new MarshalBase64().register(envelope);   // this is will over Cannot serialize: [B@f034108 

    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAPACTION, envelope);
        SoapObject response = (SoapObject) envelope.getResponse();


来源:https://stackoverflow.com/questions/12389105/runtimeexceptioncannot-serialize-error-to-send-byte-array-from-android-to-java

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