2017-03-07 69 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上找到。

随意要求澄清解决方案不明确的地方。