2012-03-06 176 views
0

我有一个应用程序连接到我们的.NET Web服务,我发现我们为每个Web服务方法创建了相同的方法,但唯一的区别是参数和Web服务方法。我正在寻找一种方法来让以下方法接受参数,然后管理1个和多个参数可能会更有用。将参数传递给SOAP方法

当前方法 * 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; 
    } 
} 

我已经尝试了一些不同的尝试,在这一点,但获得了大量的错误。我知道这很简单,但似乎无法弄清楚atm。

+0

你可以发布错误? – rodrigoap 2012-03-06 18:16:37

+0

好吧,正如我所说的,我尝试过使用数组,多维数组和其他几个数组,但是不断得到索引超出范围错误,我知道这意味着被访问的元素的索引超出范围阵列的边界,但是我对此非常沮丧,以致于我删除了所有内容并再次从头开始。非常抱歉,我没有方便的错误。 – Robert 2012-03-06 18:28:31

回答

1

我似乎已经找到了一个很好的做法,但欢迎任何反馈,如果这是好不好:

创建的类名为参数:

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); 

工程就像一个魅力!希望这可以帮助别人...