2011-06-17 21 views
5

मैं एंड्रॉइड में wsdl webservices को कॉल करने के लिए KSoap2 लाइब्रेरी का उपयोग करके WSDL webservices के लिए नया हूं।एंड्रॉइड में साबुन अनुरोध कैसे भेजें?

यह मेरा साबुन अनुरोध डंप

<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] 

कृपया किसी भी एक मुझे बताओ कैसे साबुन वस्तु में साबुन अनुरोध डंप ऊपर पारित करने के लिए कर सकते हैं त्रुटि मिल रहा है?

सादर, श्रीनिवास

उत्तर

5

आप मैन्युअल रूप से अनुरोध एक्सएमएल भी बना सकते हैं, और भेजने और प्रतिक्रिया प्रसंस्करण के लिए इसे केएसओएपी भेज सकते हैं। आप साबुनयूआई का उपयोग करके अपना अनुरोध एक्सएमएल लिख सकते हैं, फिर उन्हें 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 के साथ कस्टम एक्सएमएल अनुरोध भेजने के लिए आप अपने खुद के परिवहन वर्ग बनाने की जरूरत है।

या आप अनुरोध मैन्युअल 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 मैं उपर्युक्त को समझ नहीं पा रहा हूं क्योंकि मैं एंड्रॉइड के लिए नया हूं .. कृपया किसी भी रिफर्न्स या उदाहरण प्रदान करें। – Aravin

0

वैसा करने के लिए अपनी खुद की एक्सएमएल जनरेटर वर्ग बनाने के लिए किया है। मैं भी एक ही प्रक्रिया का उपयोग कर रहा हूँ। ksoap2 लाइब्रेरी को डीकंपाइल करें और अध्ययन करें कि आप इसे कैसे उत्पन्न करते हैं और इसे बदलते हैं ..

+0

यू कृपया elobrate कर सकते हैं क्या उर कहावत? – Srinivas

0

आप इस तरह उपयोग कर सकते हैं।

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

मुझे आशा है कि इस यू मदद मिल सकती है

धन्यवाद, चैतन्य

संबंधित मुद्दे