Android Ksoap2和WCF
我已经开发了一个WCF服务.NET4(SOAP),我正在尝试从Android上传文件。 使用.NET客户端可以很好地工作,但使用Android则不行,下面是我在Android中使用的代码 WCF服务接收数据时,流似乎为空(长度为零) 请帮帮我Android Ksoap2和WCF,android,Android,我已经开发了一个WCF服务.NET4(SOAP),我正在尝试从Android上传文件。 使用.NET客户端可以很好地工作,但使用Android则不行,下面是我在Android中使用的代码 WCF服务接收数据时,流似乎为空(长度为零) 请帮帮我 public class WCFclient { private static final String METHOD_NAME = "UploadFile"; private static final String NAMESPACE = "http:/
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;
}
这个问题已经被问过很多次了。 根据我的经验,当方法名称、名称空间、URI、Soap操作不正确时,就会出现此错误 您应该在SOAP\u操作中添加“”
private static final String SOAP_ACTION = "\"http://tempuri.org/ITransferService/UploadFile\"";
是否出现任何异常或错误??请把错误信息贴出来。
private static final String SOAP_ACTION = "\"http://tempuri.org/ITransferService/UploadFile\"";