2012-08-01 11 views
13

बनाने की कोशिश कर रहा है मैं एंड्रॉइड के लिए ksoap2 का उपयोग करके सही एसओएपी अनुरोध बनाने की कोशिश कर रहे घंटों तक संघर्ष कर रहा हूं। आदर्श अनुरोध इस तरह दिखता है:सही एसओएपी अनुरोध

<?xml version="1.0" encoding="utf-8"?> 
<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:Header> 
    <AuthorizationToken xmlns="http://www.avectra.com/2005/"> 
     <Token>string</Token> 
    </AuthorizationToken> 
    </soap:Header> 
    <soap:Body> 
    <ExecuteMethod xmlns="http://www.avectra.com/2005/"> 
     <serviceName>string</serviceName> 
     <methodName>string</methodName> 
     <parameters> 
     <Parameter> 
      <Name>string</Name> 
      <Value>string</Value> 
     </Parameter> 
     </parameters> 
    </ExecuteMethod> 
    </soap:Body> 
</soap:Envelope> 

मैं अपने अनुरोध उत्पन्न करने के लिए निम्न कोड का उपयोग कर रहा:

SoapObject request = new SoapObject(NAMESPACE, METHOD); 
    request.addProperty("serviceName", SERVICENAME); 
    request.addProperty("methodName", METHODNAME); 

    SoapObject nestedParameters = new SoapObject(NAMESPACE, "parameters"); 
    SoapObject param = new SoapObject(NAMESPACE, "Parameter"); 
    param.addProperty("Name", name); 
    param.addProperty("Value", value); 
    nestedParameters.addSoapObject(param); 
    request.addSoapObject(nestedParameters); 

    SoapSerializationEnvelope envelope = 
      new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request); 
    envelope.dotNet = true; 
    envelope.implicitTypes = true; 

    envelope.headerOut = new Element[1]; 
    Element header = new Element().createElement(NAMESPACE, "AuthorizationToken"); 
    Element token = new Element().createElement(NAMESPACE, "Token"); 
    token.addChild(Node.TEXT, this.AUTH_TOKEN); 
    header.addChild(Node.ELEMENT, token); 
    envelope.headerOut[0] = header; 

क्या ksoap2 बनाने जा रहा है है:

<v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> 
    <v:Header> 
    <n0:AuthorizationToken xmlns:n0="http://www.avectra.com/2005/"> 
     <n0:Token>string</n0:Token> 
    </n0:AuthorizationToken> 
    </v:Header> 
    <v:Body> 
    <ExecuteMethod xmlns="http://www.avectra.com/2005/" id="o0" c:root="1"> 
     <serviceName>AHAWebServices</serviceName> 
     <methodName>MemberDirectory</methodName> 
     <parameters i:type="n1:parameters" xmlns:n1="http://www.avectra.com/2005/"> 
     <Parameter i:type="n1:Parameter"> 
      <Name>string</Name> 
      <Value>string</Value> 
     </Parameter> 
     </parameters> 
    </ExecuteMethod> 
    </v:Body> 
</v:Envelope> 

मुझे लगता है कि समस्या n0 उपसर्गों के साथ शीर्षलेख में है लेकिन मुझे कोई संकेत नहीं है कि उनसे कैसे छुटकारा पाएं। मैंने उन्हें सरल से टाइप करने के लिए शरीर से हटा दिया लेकिन मुझे हेडर के लिए एक समान सेटिंग नहीं मिल रही है। मैं SOAP के लिए नया हूं इसलिए किसी भी अन्य सलाह की सराहना की जाती है। क्या किसी के पास कोई विचार है कि मैं इसे कैसे ठीक कर सकता हूं?

उत्तर

5

KSOAP का उपयोग करते समय इस के लिए मुझे

SoapObject request = new SoapObject(WEBSERVICE_NAMESPACE, methodName); 
    if(null != parameterMap && !parameterMap.isEmpty()){ 
     for(Entry<String, String> entry: parameterMap.entrySet()){ 
      request.addProperty(entry.getKey(), entry.getValue()); 
     } 
    } 
    // Declare the version of the SOAP request 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.implicitTypes = true; 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(ApplicationConstants.WEBSERVICE_WSDL_URL); 

    // this is the actual part that will call the webservice 
    try { 

     androidHttpTransport.debug = true; 
     androidHttpTransport.call(soapActionUrl, envelope); 
     String ss = androidHttpTransport.responseDump; 

     // Get the SoapResult from the envelope body. 

     Log.d(TAG, "request: " + androidHttpTransport.requestDump); 
     Log.d(TAG, "response: "+ androidHttpTransport.responseDump); 


     SoapObject result = (SoapObject) envelope.getResponse(); 

     Log.d("soap response", "" + result);    
    } catch (IOException e) { 
     Log.e(TAG, "IOException", e); 
    } 

नोट काम किया:

androidHttpTransport.debug = true; 

मेरे मामले में समस्या हल हो गई। मेरे सिर को टक्कर लगी लेकिन तर्क नहीं दे सका कि इस मुद्दे को हल करने में मदद क्यों हुई।

आपको ksoap का उपयोग करने की आवश्यकता क्यों है? बस स्ट्रिंग के रूप में अपने एसओएपी अनुरोध की स्थिर पार्टी है, मूल्यों को स्थिर भाग में संलग्न करें और अंततः आप पूर्ण SOAP अनुरोध प्राप्त कर सकते हैं। अंत में अपना पोस्ट अनुरोध भेजने के लिए HTTP विधियों का उपयोग करें।

कोई अतिरिक्त जार

इसके अलावा ksoap बड़ी प्रतिक्रिया के लिए OOM जैसे मुद्दों है, आदि

KSOAP OOM issue

आप नीचे दिए गए कोड

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.HashMap; 
import java.util.Map.Entry; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 

import android.util.Log; 

public final class SOAPRequest{ 

private static final String TAG = "SOAPRequest"; 
private static final String TAG_SOAP_HEADER_START = "<?xml version=\"1.0\" encoding=\"utf-8\"?><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:Header>"; 
private static final String TAG_AUTHORIZATION_START = "<AuthorizationToken xmlns=\"http://www.avectra.com/2005/\">"; 
private static final String TAG_TOKEN_START = "<TOKEN>"; 
private static final String TAG_TOKEN_END = "</TOKEN>"; 
private static final String TAG_AUTORIZATION_END = "</AuthorizationToken>"; 
private static final String TAG_SOAPHEADER_END = "</soap:Header>"; 
private static final String TAG_SOAP_BODY_START = "<soap:Body>"; 
private static final String TAG_PARAM_NAME_START = "<Name>"; 
private static final String TAG_PARAM_NAME_END = "</Name>"; 
private static final String TAG_PARAM_VALUE_START = "<Value>"; 
private static final String TAG_PARAM_VALUE_END = "</Value>"; 
private static final String TAG_METHOD_START = "<methodName>"; 
private static final String TAG_METHOD_END = "</methodName>"; 
private static final String TAG_SERVICE_START = "<serviceName>"; 
private static final String TAG_SERVICE_END = "</serviceName>"; 
private static final String TAG_PARAMS_START = "<parameters><Parameter>"; 
private static final String TAG_EXE_METHOD_START = "<ExecuteMethod xmlns=\"http://www.avectra.com/2005/\">"; 
private static final String TAG_SOAP_REQ_END = "</Parameter></parameters></ExecuteMethod></soap:Body></soap:Envelope>"; 

/** 
* Constructor intentionally made private 
*/ 
private SOAPRequest() { 

} 
/** 
* Builds a SOAP request with the specified value 
* @param token Value of token 
* @param serviceName Value of servicename 
* @param methodName Value of methodName 
* @param paramsMap Collection of parameters as set of name value pair which needs to be sent 
* @return the complete soap request 
*/ 
public static String buildRequest(String token, String serviceName, String methodName, HashMap<String, String> paramsMap){ 
    StringBuilder requestBuilder = new StringBuilder(TAG_SOAP_HEADER_START); 
    requestBuilder.append(TAG_AUTHORIZATION_START); 
    requestBuilder.append(TAG_TOKEN_START); 
    requestBuilder.append(token); 
    requestBuilder.append(TAG_TOKEN_END); 
    requestBuilder.append(TAG_AUTORIZATION_END); 
    requestBuilder.append(TAG_SOAPHEADER_END); 
    requestBuilder.append(TAG_SOAP_BODY_START); 
    requestBuilder.append(TAG_EXE_METHOD_START); 
    requestBuilder.append(TAG_SERVICE_START); 
    requestBuilder.append(serviceName); 
    requestBuilder.append(TAG_SERVICE_END); 
    requestBuilder.append(TAG_METHOD_START); 
    requestBuilder.append(methodName); 
    requestBuilder.append(TAG_METHOD_END); 
    requestBuilder.append(TAG_PARAMS_START); 
    for(Entry<String, String> param :paramsMap.entrySet()){ 
     requestBuilder.append(TAG_PARAM_NAME_START); 
     requestBuilder.append(param.getKey()); 
     requestBuilder.append(TAG_PARAM_NAME_END); 
     requestBuilder.append(TAG_PARAM_VALUE_START); 
     requestBuilder.append(param.getValue()); 
     requestBuilder.append(TAG_PARAM_VALUE_END); 
    } 
    requestBuilder.append(TAG_SOAP_REQ_END); 
    return requestBuilder.toString(); 
} 

/** 
* Connection timeout set for the HttpClient 
*/ 
private static final int CONNECTION_TIMEOUT= 6000; 
/** 
* Socket timeout set for the HttpClient 
*/ 
private static final int SOCKET_TIMEOUT = 10000; 

/** 
* @return httpClient An instance of {@link DefaultHttpClient} 
*/ 
private static DefaultHttpClient getHttpClient() { 
    HttpParams httpParameters = new BasicHttpParams(); 
    // Set the timeout in milliseconds until a connection is established. 
    // The default value is zero, that means the timeout is not used. 
    HttpConnectionParams.setConnectionTimeout(httpParameters,CONNECTION_TIMEOUT); 
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data. 
    HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT); 

    return new DefaultHttpClient(httpParameters); 
} 

/** 
* Sends a SOAP request to the specified service endpoint. 
* 
* @param serviceEndpoint The service endpoint which will be hit 
* @param soapRequest The SOAP request 
* @return The string representing the response for the specified SOAP request. 
*/ 
public static String send(String serviceEndpoint, String soapRequest){ 
    HttpPost httppost = new HttpPost(serviceEndpoint);   
    StringEntity se = null; 
    try { 
     se = new StringEntity(soapRequest,HTTP.UTF_8); 
    } catch (UnsupportedEncodingException e) { 
     Log.e(TAG,"send", e); 
     return null; 
    } 

    se.setContentType("text/xml"); 
    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); 
    httppost.setEntity(se); 
    String result = null; 
    HttpClient httpclient = getHttpClient(); 
    try { 
     HttpResponse httpResponse = httpclient.execute(httppost); 
     HttpEntity responseEntity = httpResponse.getEntity(); 
     if(null!= responseEntity){ 
      //if you have a huge chunk of data read it using a buffer 
      result =EntityUtils.toString(responseEntity); 
     } 
    } catch (ClientProtocolException e) { 
     Log.e(TAG,"send", e); 
    } catch (IOException e) { 
     Log.e(TAG,"send", e); 
    } catch (Exception e){ 
     Log.e(TAG,"send", e); 
    } 

    return result; 
} 

} 
+0

क्या आप कृपया मुझे साबुन के बारे में बता सकते हैं और मूल्य इस पर गुजरते हैं .... –

1

मुझे लगता है कि आपको हेडर बनाने के लिए एक और तरीका चाहिए, यह जैक्स-डब्ल्यू जैसा दिखता है, इसलिए मैं कुछ महीने पहले एक जैक्स डब्ल्यूएस कार्यान्वयन के साथ जाऊंगा। आप शीर्ष लेख रचना को संभालने के लिए एक HeaderHandlerResolver बना सकते हैं और एक में डालने उसके बाद


import javax.xml.namespace.QName; 
    import javax.xml.soap.SOAPElement; 
    import javax.xml.soap.SOAPEnvelope; 
    import javax.xml.soap.SOAPHeader; 
    import javax.xml.ws.handler.MessageContext; 
    import javax.xml.ws.handler.soap.SOAPHandler; 
    import javax.xml.ws.handler.soap.SOAPMessageContext; 


    public class HeaderHandler implements SOAPHandler<SOAPMessageContext> { 

     public boolean handleMessage(SOAPMessageContext smc) { 
      Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
      String AUTH_TK = "http://www.avectra.com/2005/"; 
      String PREFIX="";//no prefix 
      String PREFIX_XMLNS="xmlns"; 
      String value = "123456"; 
      if (outboundProperty.booleanValue()) { 
       try { 
        SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope(); 
        SOAPHeader header = envelope.addHeader(); 
        //<AuthorizationToken xmlns="http://www.avectra.com/2005/"> 
        SOAPElement authorizationToken = header.addChildElement("AuthorizationToken", PREFIX_XMLNS, AUTH_TK); 
        //<Token>value</Token> 
        SOAPElement usernameToken = 
         authorizationToken.addChildElement("Token", PREFIX); 
         usernameToken.addTextNode(value); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return outboundProperty; 
     } 


     public Set<QName> getHeaders() { 
      return null; 
     } 

     public void close(MessageContext arg0) { 

     } 

     public boolean handleFault(SOAPMessageContext arg0) { 
      return false; 
     } 
    } 

:

सबसे पहले आप एक HeaderHandler वर्ग की जरूरत है, जो साबुन हैडर तत्व बनाता है, जिसे इस तरह दिखना चाहिए हैंडलर श्रृंखला:


import java.util.ArrayList; 
    import java.util.List; 
    import javax.xml.ws.handler.Handler; 
    import javax.xml.ws.handler.HandlerResolver; 
    import javax.xml.ws.handler.PortInfo; 

    public class HeaderHandlerResolver implements HandlerResolver { 

    @SuppressWarnings("unchecked") 
    public List<Handler> getHandlerChain(PortInfo portInfo) { 
      List<Handler> handlerChain = new ArrayList<Handler>(); 
      HeaderHandler hh = new HeaderHandler(); 
      handlerChain.add(hh); 
      return handlerChain; 
     } 
    } 

उसके बाद, आप क्लाइंट में जोड़ें:


 try{ 
      //new service instance (your service should be extending javax.xml.ws.Service;) 
      YourServiceProxy service = new YourServiceProxy(); 
      //calls the header handler resolver ;) 
      service.setHandlerResolver(new HeaderHandlerResolver()); 
      //get the service 
      YourService port = (YourService)service.getYourService(); 
      //call the service 
      port.yourMethod() 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

वैसे, मैं इस विशेष हैडर का परीक्षण नहीं किया है, मैं पिछले एक हैडर हैंडलर मैं था संशोधित, तो यह सही नहीं हो सकता है, लेकिन मुझे लगता है कि यह बहुत करीब है, मैं सच में यह आशा आपकी मदद करता है, इसे आज़माएं और हमें बताएं कि यह कैसा आता है, अगर मैं अभी भी काम नहीं करता हूं तो मैं आपकी मदद करने की कोशिश करूंगा।

0

आप की जाँच की है कि क्या के लिए parameters (अर्थात i:type="n1:parameters") kSOAP द्वारा उत्पन्न प्रकार और Parameter (अर्थात i:type="n1:Parameter") नोड्स सही हैं (वे wsdl में परिभाषित कर रहे हैं) है?

कोशिश

envelope.implicitTypes = true; 

की स्थापना और यह भी साथ

envelope.setAddAdornments(false); 

kSOAP प्रकार विशेषता और नाम रिक्तियां शामिल न मजबूर करने के लिए खेलते हैं।

0

सेट

उपयोग कर सकते हैं
envelope.implicitTypes = true; 

और

envelope.setAddAdornments(false) 

यह मेरे लिए काम किया सेट न करें।

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