2017-03-07 105 views
-1

我是SOAP的新開發者,所以引導我如何在android中調用soap api。 這裏是我發現的方法,但不知道它是如何工作的。我已經下載thisAndroid和SOAP api的連接

public static String connectSOAP(String url, String soapNamespace, String soapAction, String soapMethod, Hashtable<String, String> HT) { 

     String result = null; 

     SoapObject request = new SoapObject(soapNamespace, soapMethod); 

     if (HT != null) { 
      Enumeration<String> en = HT.keys(); 
      while (en.hasMoreElements()) { 

       Object k = en.nextElement(); 
       Object v = HT.get(k); 
       request.addProperty(k.toString(), v); 
       System.out.println("key = " + k.toString() + "; value = " + v); 
      } 
     } 


     SoapSerializationEnvelope envelope = 
       new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     //envelope.bodyOut = request; 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 

     HttpTransportSE androidHttpTransport = new HttpTransportSE(url); 
     //envelope.getResponse(); 


     try { 

      androidHttpTransport.call(soapAction, envelope); 

      if (envelope.bodyIn instanceof SoapFault) { 

       result = ((SoapFault) envelope.bodyIn).faultstring; 


      } else { 

       SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 
       result = resultsRequestSOAP.getProperty(0).toString(); 
      } 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return result; 
    } 

這是方法,我想作爲一個API調用來使用,但不知道怎麼用這個。 所以請幫助我。

+0

[這裏](https://code.tutsplus.com/tutorials/consuming-web-services-with-ksoap--mobile-21242)你將會逐步獲得指令。 – sodhankit

+0

好的,謝謝@sodhankit。如果我有任何問題,我會回到這一點。 –

回答

0

在使用soap webservice時,我強烈建議你不要使用任何庫來發出肥皂請求。使用android提供的類更方便,不易出錯。這就是您如何在不使用庫的情況下發出soap請求:首先,您需要知道如何使用作爲Windows應用程序的SOAP Ui。你可以在這裏導入你的wsdl文件,如果它的語法正確,那麼你會得到一個屏幕顯示你的web服務的請求正文。你可以輸入測試值,你會得到一個響應結構。此鏈接將指導您如何現在就用肥皂UI https://www.soapui.org/soap-and-wsdl/working-with-wsdls.html

到androiod代碼:

我們將創建一個名爲runTask類擴展異步任務,並使用HTTP發送請求主體和獲取請求響應:

private class runTask extends AsyncTask<String, String, String> { 

     private String response; 
     String string = "your string parameter" 
     String SOAP_ACTION = "your soap action here"; 

     String stringUrl = "http://your_url_here"; 
     //if you experience a problem with url remove the '?wsdl' ending 



     @Override 
     protected String doInBackground(String... params) { 

      try { 

         //paste your request structure here as the String body(copy it exactly as it is in soap ui) 
         //assuming that this is your request body 


       String body = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" + 
           "<soap:Body>"+ 
           "<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">"+ 
           "<GetCitiesByCountryResult>"+string+"</GetCitiesByCountryResult>"+ 
           "</GetCitiesByCountryResponse>"+ 
           "</soap:Body>"+ 
           "</soap:Envelope>"; 


       try { 
        URL url = new URL(stringUrl); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setRequestMethod("POST"); 
        conn.setDoOutput(true); 
        conn.setDefaultUseCaches(false); 
        conn.setRequestProperty("Accept", "text/xml"); 
        conn.setRequestProperty("SOAPAction", SOAP_ACTION); 

        //push the request to the server address 

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
        wr.write(body); 
        wr.flush(); 

        //get the server response 

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        StringBuilder builder = new StringBuilder(); 
        String line = null; 

        while ((line = reader.readLine()) != null) { 


         builder.append(line); 
         response = builder.toString();//this is the response, parse it in onPostExecute 

        } 


       } catch (Exception e) { 

        e.printStackTrace(); 
       } finally { 

        try { 

         reader.close(); 
        } catch (Exception e) { 

         e.printStackTrace(); 
        } 
       } 


      } catch (Exception e) { 

       e.printStackTrace(); 
      } 

      return response; 
     } 

     /** 
     * @see AsyncTask#onPostExecute(Object) 
     */ 
     @Override 
     protected void onPostExecute(String result) { 



      try { 

       Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

現在只是執行類,並觀看魔術發生:

runTask runner = new runTask(); 
runner.execute(); 

得到您的回覆後,就可以比肩使用DOM或SAX解析,一些小小的研究,你就會明白如何解析。您可以使用java創建WSDL web服務,並使用Eclipse Ide。在WSDL Web服務上,教程也可以在youtube上找到。

隨意要求澄清解決方案不明確的地方。