我已經開發了一個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
有任何異常或錯誤發生?請發佈錯誤消息。 –