2012-03-06 22 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; 
    } 
} 

私はこれで異なる試みのカップルを試みたが、エラーの多くを取得しました。私はそれがむしろシンプルだと知っていますが、それを気づかないようです。

+0

あなたはエラーを公表できますか? – rodrigoap

+0

さて、私が言ったように、配列、多次元配列、およびその他のものでこれを試しましたが、アクセスしている要素のインデックスが私はすべてを削除し、最初から始めました。だから申し訳ありませんが、私は間違いがありません。 – Robert

答えて

1

私は素敵なアプローチを考え出しているように見えるが、これはOKであるかどうかについてどのようなフィードバックを歓迎:

作成したクラスと呼ばれるパラメータ:

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

魅力的な作品です!これが他の誰かを助けることを願って...

関連する問題