我已经开发了一个WCF服务.NET4(SOAP)和我试图上传文件于Android。 随着.NET客户端做工精细,但与Android不会工作,下面的代码我使用Android系统。的Android Ksoap2和WCF
的WCF服务recive中的数据,该流似乎是空(零长度)。
请帮帮我。
public class WCFclient {
private static final String METHOD_NAME = "UploadFile";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.103/AndroidWCF/Service1.svc";
private static final String SOAP_ACTION = "http://tempuri.org/ITransferService/UploadFile";
public static String extractText(byte[] _data)
{
@SuppressWarnings("unused")
int lun = _data.length;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
MarshalBase64 marshal = new MarshalBase64();
PropertyInfo p1=new PropertyInfo();
p1.setName("request");
p1.setType(_data);
request.addProperty(p1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
envelope.setOutputSoapObject(request);
marshal.register(envelope);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 20000);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SoapObject result = null;
try {
result = (SoapObject)envelope.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String resultData=result.getProperty(0).toString();
return resultData;
}
// Returns the contents of the file in a byte array.
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;
}
}
在WCF服务时发生错误,例外的是:“开始从元素命名空间‘请求’‘http://tempuri.org/’从命名空间预期找到的元素‘图像’。 “http://tempuri.org/” 1号线,位置304”
Public Sub UploadFile(request As Stream) Implements ITransferService.UploadFile
Dim writeStream As FileStream = New FileStream("c:\" & Now.Ticks.ToString & ".bmp", FileMode.Create, FileAccess.Write)
ReadWriteStream(request, writeStream)
End Sub
Private Sub ReadWriteStream(readStream As Stream, writeStream As Stream)
Dim Length As Integer = 256
Dim buffer As [Byte]() = New [Byte](Length - 1) {}
Dim bytesRead As Integer = readStream.Read(buffer, 0, Length)
' write the required bytes
While bytesRead > 0
writeStream.Write(buffer, 0, bytesRead)
bytesRead = readStream.Read(buffer, 0, Length)
End While
readStream.Close()
writeStream.Close()
End Sub
有任何异常或错误发生?请发布错误消息。 –