我必须将Byte数组发送到我的java应用程序来自Android设备。我在android中使用ksoap2,我在java中使用axis2创建了web服务,以接收该字节数组并在服务器上创建文件以进行进一步处理。 / p>
让我给你完整的解释。我在android设备中记录一个wave文件,然后该wave文件需要在server.i上的java应用程序中发送。我已经在java应用程序中创建了一个web2服务,其中axis2名称为“ get_wave_byte“将传入的字节数组再次转换为wave文件,并将其存储在server.and android设备中我将wave文件作为字节数组发送,并将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 < 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;
}
所以基本上我现在用以下代码将这些字节数组发送到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);
从这里我使用ksoap2来调用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 Application方法是我从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);
}
简单就像冠军一样工作并轻松存储我的波形文件
那么我的android代码中的问题是它给出了错误。
你可以告诉我哪里出错了?提前谢谢你。
答案 0 :(得分:13)
好的。我用
解决了我的错误SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
new MarshalBase64().register(envelope); // serialization
答案 1 :(得分:0)
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();