Android: Ksoap 2传递参数array作为参数
本文关键字:参数 array Ksoap Android | 更新日期: 2023-09-27 18:13:26
是否有可能为Android KSoap2传递值与参数数组的webservice方法?如果是,怎么做?
For VB: Public Function calcSum(ByVal ParamArray args() As Double) As Double
为c# public static Double calcSum(params int[] args)
应该像这样正常传递参数吗?
SoapObject request = new SoapObject(NAMESPACE + "/", methodCalcSumService);
request.addProperty("args", intArg1);
request.addProperty("args", intArg1); //2nd parameter, will this be overriden?
envelope.setOutputSoapObject(request);
androidHttpTransport.call(soapCalcSumService, envelope);
Object result = envelope.getResponse(); //Get XML Result
谢谢你
我需要将String[]发送给VB Web Service方法
<WebMethod()> _
Public Function SendByteArrayString(ByVal ImageNo As String, ByVal Name As String, ByVal ParamArray ArrImage() As String) As Boolean
这段代码对我有效。希望这对你有帮助!
/** This class serialized String Array for passing PropertyInfo to Web Service
* Got this from hasanghaforian's answer (http://stackoverflow.com/questions/7130862/how-to-pass-string-array-to-webservice-using-ksoap2)
*/
public class SerializableStringArray extends Vector<String> implements KvmSerializable
{
private static final long serialVersionUID = -1166006770093411055L;
private int intPropertyCount = 1;
@Override
public Object getProperty(int arg0)
{
return this.get(arg0);
}
@Override
public int getPropertyCount()
{
return intPropertyCount;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2)
{
arg2.name = "string";
arg2.type = PropertyInfo.STRING_CLASS;
}
@Override
public void setProperty(int arg0, Object arg1)
{
this.add(arg1.toString());
}
@Override
public void setSize(int arg0)
{
intPropertyCount = arg0;
}
}
public final Boolean SendByteArrayString(String strServiceCallNo, String DBName, Bitmap[] bmp)
{
soapSendByteArrayString = NAMESPACE + "/" + methodSendByteArrayString;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60000);
boolean isValid = false;
try
{
isSuccessful = false;
SoapObject request = new SoapObject(NAMESPACE + "/", methodSendByteArrayString);
request.addProperty("ImageNo", strServiceCallNo);
request.addProperty("Name", strDBName);
SerializableStringArray serializedStringArray = new SerializableStringArray();
for (int i = 0; i < bmp.length; i++)
{
try
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp[i].compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
serializedStringArray.add(new String(Base64.encodeBase64(byteArray, false), "UTF-8"));
BusinessRules.appendLog("[ArrImage_" + i + " ]" + new String(Base64.encodeBase64(byteArray, false), "UTF-8"));
}
catch (Exception e)
{
e.toString();
}
}
serializedStringArray.setSize(bmp.length);
PropertyInfo propertyInfoStringArray = new PropertyInfo();
propertyInfoStringArray.setName("ArrImage");
propertyInfoStringArray.setValue(serializedStringArray);
propertyInfoStringArray.setType(String[].class); //Pass as a String[] class
request.addProperty(propertyInfoStringArray);
envelope.setOutputSoapObject(request);
Object result = "false";
try
{
envelope.addMapping(NAMESPACE, "ArrImage", new SerializableStringArray().getClass());
androidHttpTransport.call(soapSendByteArrayString, envelope);
result = envelope.getResponse(); //Get XML Result
}
catch (Exception e)
{
e.printStackTrace();
BusinessRules.appendLog("[WebService.java + SendByteArrayString + envelope.getResponse() ]" + e.toString());
}
isValid = Boolean.valueOf(result.toString());
}
catch (Exception e)
{
isValid = false;
e.printStackTrace();
BusinessRules.appendLog("[WebService.java + SendByteArrayString ]" + e.toString());
}
return isValid;
}