我有一个连接到我们的.NET Web服务的应用程序,我发现我们正在为每个Web服务方法创建相同的方法,但唯一的区别是参数和Web服务方法。我正在寻找一种方法让下面的方法接受参数,然后管理1对几个更有用。
当前方法 * NameSpace,URL,argName,argValue都在类的顶部定义。
public static Document GetTickets() {
try {
SoapObject request = new SoapObject(NameSpace, "GetTickets");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
request.addProperty(argName, argValue);
request.addProperty("Customer", "ABC");
request.addProperty("Local", "USA");
envelope.setOutputSoapObject(request);
androidHttpTransport.call("RemoteWebService/GetTickets", envelope);
SoapPrimitive responseData = (SoapPrimitive) envelope.getResponse();
if (responseData != null)
{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
InputSource is = new InputSource(new StringReader(responseData.toString()));
return db.parse(is);
}
else
return null;
}
catch (Exception e)
{
Errors.LogError(e);
return null;
}
}
我希望它能成为这样的东西:
public static Document GetTickets(String WebServiceMethod, ArrayList Params) {
try {
SoapObject request = new SoapObject(NameSpace, WebServiceMethod);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//Syntax is wrong, I know, but just want to show what I'm looking to do:
foreach(Parameter p in Params)
request.addProperty(p[0].value, p[1].value);
envelope.setOutputSoapObject(request);
androidHttpTransport.call("RemoteWebService/" + WebServiceMethod, envelope);
SoapPrimitive responseData = (SoapPrimitive) envelope.getResponse();
if (responseData != null)
{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
InputSource is = new InputSource(new StringReader(responseData.toString()));
return db.parse(is);
}
else
return null;
}
catch (Exception e)
{
Errors.LogError(e);
return null;
}
}
我尝试了几次不同的尝试,但却遇到了很多错误。我知道这很简单,但似乎无法解决这个问题。
答案 0 :(得分:1)
我似乎已经找到了一个很好的方法,但欢迎任何反馈,如果这是好的:
创建了一个名为Parameter:
的类public class Parameter {
private String mParameterName;
private String mParameterValue;
// [[ ParameterName
public void setParameterName(String ParameterName){
mParameterName = ParameterName;
}
public String getParameterName(){
return mParameterName;
}
// ]]
// [[ ParameterValue
public void setParameterValue(String ParameterValue){
mParameterValue = ParameterValue;
}
public String getParameterValue(){
return mParameterValue;
}
// ]]
}
然后修改我的方法以接受此类型的列表:
public static Document GetWebServiceData(String WebServiceMethod, List<Parameter> Params) {
try {
SoapObject request = new SoapObject(NameSpace, WebServiceMethod);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
for(Parameter param : Params)
request.addProperty(param.getParameterName(), param.getParameterValue());
envelope.setOutputSoapObject(request);
androidHttpTransport.call(NameSpace + "/" + WebServiceMethod, envelope);
SoapPrimitive responseData = (SoapPrimitive) envelope.getResponse();
if (responseData != null)
{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
InputSource is = new InputSource(new StringReader(responseData.toString()));
return db.parse(is);
}
else
return null;
}
catch (Exception e)
{
Errors.LogError(e);
return null;
}
}
然后它就像这样被访问:
List<Parameter> Params = new ArrayList<Parameter>();
Parameter Param = new Parameter();
Param.setParameterName("Customer");
Param.setParameterValue("ABC");
Params.add(Param);
Param = new Parameter();
Param.setParameterName("Local");
Param.setParameterValue("USA");
Params.add(Param);
Document doc = GetWebServiceData("GetTickets", Params);
像魅力一样!希望这有助于其他人...