RunTimeException:无法序列化:通过ksoap将字节数组从android发送到java应用程序时出错

我必须将Byte数组发送到我的java应用程序来自Android设备。我在android中使用kso​​ap2,我在java中创建了带有axis2的web服务,以接收该字节数组并在服务器上创建文件以供进一步处理。

让我给你全部解释。我在android设备中记录一个wave文件,然后该wave文件需要在server.i上的java应用程序中发送。我在java应用程序中创建了一个web2服务,其中axis2名称为“get_wave_byte”将传入的字节数组再次转换为波形文件并将其存储在server.and从android设备我发送波形文件作为字节数组以及get_wave_byte()参数中的存储路径。

因此,我已经在android中创建了wave文件,之后我创建了一个将该wave文件转换为byte array的方法。在字节数组中转换wave文件的方法如下…

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

所以基本上我现在用以下代码将这些字节数组发送到java应用程序…

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

//创建临时字节数组以在字节数组中存储这些文件

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

//然后调用我的方法,如上所述,以字节数组转换文件

 temp=getBytesFromFile(source_for_byte); 

从这里我使用kso​​ap2来调用java应用程序方法,其中那些数组字节作为参数,并且字符串作为文件路径以使用get_wav_byte()方法存储在服务器上

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

我的java应用程序方法是我从android调用如下…

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

我的问题是,当我运行我的Android应用程序来调用此Web服务时,它给了我以下错误

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

更重要的是我想告诉我,我使用以下代码从我的.net应用程序调用相同的Java方法。

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

而且它的工作方式就像Champ一样,可以轻松存储我的wave文件

那么我的android代码中的问题是什么呢?它给出了错误。

你能告诉我什么出错吗?

先谢谢你。

好的,我解决了我的错误

 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); new MarshalBase64().register(envelope); // serialization 
  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();