如何在android中发送soap请求?

如何在android中发送soap请求?

问题描述:

我是WSDL webservices的新手,使用KSoap2库在android中调用wsdl webservices。如何在android中发送soap请求?

这是我的SOAP请求转储

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:loy="http://loyalcard.com/LoyalCardWebService/">; 
    <soapenv:Header/> 
    <soapenv:Body> 
     <loy:GetOffersByLocation> 
     <!--Optional:--> 
     <loy:Location> 
      <!--Optional:--> 
      <loy:Latitude>?</loy:Latitude> 
      <!--Optional:--> 
      <loy:Longitude>?</loy:Longitude> 
     </loy:Location> 
     </loy:GetOffersByLocation> 
    </soapenv:Body> 
</soapenv:Envelope> 

我传递这SopaObject像:

PropertyInfo latitude = new PropertyInfo(); 
     latitude.name="Latitude"; 
     latitude.type=Double.class; 
     latitude.setValue(32.806673); 

    PropertyInfo longitude = new PropertyInfo(); 
     longitude.name="Longitude"; 
     longitude.type=Double.class; 
     longitude.setValue(-86.791133); 

     SoapObject results = null; 
     String methodName = "OffersByLocation"; 
     String actionName = "http://loyalcard.com/LoyalCardWebService/GetOffersByLocation"; 
     SoapObject request = new SoapObject(NAMESPACE,methodName); 

     request.addProperty(latitude); 
     request.addProperty(longitude); 

我在这里通过经度和纬度值直接OffersByLocation,我应该通过元素位置。请任何人都可以帮助如何通过位置传递参数。

我试图与上述过程,但我得到错误说

06-17 11:52:55.934: WARN/System.err(350): SoapFault - faultcode: 'soapenv:Server' faultstring: 'org.apache.axis2.databinding.ADBException: Unexpected subelement Latitude' faultactor: 'null' detail: [email protected] 

请任何一个可以告诉我如何通过上述SOAP对象的SOAP请求转储?

问候, SRINIVAS

您也可以手动构建请求XML,并将其发送到kSOAP进行发送和响应处理。您可以使用soapUI编写您的请求XML,然后将它们保存在res/raw中,其关键字如{%key%}参数应放置在运行时。 这里是更换关键字代码:

// parse the template and replace all keywords 
StringBuffer sb = new StringBuffer(); 
try { 
    // find all keywords 
    Pattern patern = Pattern.compile("\\{%(.*?)%\\}"); 
    Matcher matcher = patern.matcher(templateHtml); 

    while (matcher.find()) { 
    String keyName = matcher.group(1); 
    String keyValue = values.get(keyName); 
    if (keyValue == null) { 
     keyValue = ""; 
    } 
    // replace the key with value 
    matcher.appendReplacement(sb, keyValue); 
    } 
    matcher.appendTail(sb); 

    // return the final string 
    return sb.toString(); 
} catch (Throwable e) { 
    Log.e(LOG_TAG, "Error parsing template", e); 
    return null; 
} 

要与KSOAP发送自定义XML请求,你需要使自己的交通运输类。

或者您可以使用DefaultHttpClient(请参阅Using client/server certificates for two way authentication SSL socket on Android)手动发送请求,并使用kSOAP来解析响应。

/** 
    * Sends SOAP request to the web service. 
    * 
    * @param requestContent the SOAP request XML 
    * @return KvmSerializable object generated from the SOAP response XML 
    * @throws Exception if the web service can not be 
    * reached, or the response data can not be processed. 
    */ 
    public Object sendSoapRequest(String requestContent) 
     throws Exception { 

    // send SOAP request 
    InputStream responseIs = sendRequest(requestContent); 

    // create the response SOAP envelope 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    // process SOAP response 
    parseResponse(responseIs, envelope); 

    Object bodyIn = envelope.bodyIn; 
    if (bodyIn instanceof SoapFault) { 
     throw (SoapFault) bodyIn; 
    } 

    return bodyIn; 
    } 

    /** 
    * Sends SOAP request to the web service. 
    * 
    * @param requestContent the content of the request 
    * @return {@link InputStream} containing the response content 
    * @throws Exception if communication with the web service 
    * can not be established, or when the response from the service can not be 
    * processed. 
    */ 
    private InputStream sendRequest(String requestContent) throws Exception { 

    // initialize HTTP post 
    HttpPost httpPost = null; 
    try { 
     httpPost = new HttpPost(serviceUrl); 
     httpPost.addHeader("Accept-Encoding", "gzip,deflate"); 
     httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8"); 
     httpPost.addHeader("SOAPAction", "\"\""); 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error initializing HTTP post for SOAP request", e); 
     throw e; 
    } 

    // load content to be sent 
    try { 
     HttpEntity postEntity = new StringEntity(requestContent); 
     httpPost.setEntity(postEntity); 
    } catch (UnsupportedEncodingException e) { 
     Log.e(LOG_TAG, "Unsupported ensoding of content for SOAP request", e); 
     throw e; 
    } 

    // send request 
    HttpResponse httpResponse = null; 
    try { 
     httpResponse = httpClient.execute(httpPost); 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error sending SOAP request", e); 
     throw e; 
    } 

    // get SOAP response 
    try { 
     // get response code 
     int responseStatusCode = httpResponse.getStatusLine().getStatusCode(); 

     // if the response code is not 200 - OK, or 500 - Internal error, 
     // then communication error occurred 
     if (responseStatusCode != 200 && responseStatusCode != 500) { 
     String errorMsg = "Got SOAP response code " + responseStatusCode + " " 
      + httpResponse.getStatusLine().getReasonPhrase(); 
     ... 
     } 

     // get the response content 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream is = httpEntity.getContent(); 
     return is; 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error getting SOAP response", e); 
     throw e; 
    } 
    } 

    /** 
    * Parses the input stream from the response into SoapEnvelope object. 
    */ 
    private void parseResponse(InputStream is, SoapEnvelope envelope) 
     throws Exception { 
    try { 
     XmlPullParser xp = new KXmlParser(); 
     xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 
     xp.setInput(is, "UTF-8"); 
     envelope.parse(xp); 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error reading/parsing SOAP response", e); 
     throw e; 
    } 
    } 
+0

Peceps我无法理解上面的一个,因为我是一个新的android..please提供任何引用或例子在那里。 – Aravin 2013-09-27 11:59:53

你必须使自己的XML生成类来做到这一点。我也使用相同的程序。反编译ksoap2库并研究它们如何生成并根据需要进行更改。

+0

u能请elobrate什么乌尔说什么? – Srinivas 2011-06-17 08:11:56

你可以这样使用。

SoapObject requestObj=new SoapObject(NAMESPACE,"GetOffersByLocation"); 

SoapObject locationObj=new SoapObject(NAMESPACE,"Location"); 

PropertyInfo latitude = new PropertyInfo(); 
        latitude.name="Latitude"; 
       latitude.type=Double.class; 
       latitude.setValue(32.806673); 
       locationObj.addProperty(latitude); 

     PropertyInfo longitude = new PropertyInfo(); 
       longitude.name="Longitude"; 
       longitude.type=Double.class; 
       longitude.setValue(-86.791133); 

     locationObj.addProperty(longitude); 
     requestObj.addSoapObject(locationObj); 

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     envelope.setOutputSoapObject(requestObj); 
     envelope.dotNet = false; 
     envelope.bodyOut = request; 
     envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
     int timeout = 60000; 
    String URL="www..........wsdl"; 
     httpTransportSE = new HttpTransportSE(URL, 
     timeout); 
     httpTransportSE.debug = true; 
     Log.v("request", request.toString()); 
     httpTransportSE.call(actionName, envelope); 

我希望这可以帮助ü

感谢, 切塔尼亚