2012-02-22 180 views
4

我需要生成一个像这样的SOAP请求。使用KSOAP创建SOAP请求Android

SOAP-REQUEST

POST /TennisMasters/TennisMasters.Listener.asmx HTTP/1.1 
Host: playinkstudio.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://playinktennismasters.com/authenticateUser" 

    <?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:Body> 
     <authenticateUser xmlns="http://playinktennismasters.com/"> 
      <user>string</user> 
     </authenticateUser> 
     </soap:Body> 
    </soap:Envelope> 

我使用KSOAP2,建立这个请求。

private static String SOAP_ACTION = "http://playinktennismasters.com/authenticateUser"; 
private static String NAMESPACE = "http://playinktennismasters.com/"; 
private static String METHOD_NAME = "authenticateUser"; 
private static String URL = "http://playinkstudio.com/TennisMasters/TennisMasters.Listener.asmx"; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("user", "A Json String will be here"); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER12); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.debug = true; 
    try { 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

这是我从调试中得到的请求。

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope"> 
<v:Header /> 
<v:Body> 
<authenticateUser xmlns="http://playinktennismasters.com/" **id="o0" c:root="1"**> 
<user **i:type="d:string"**>{"email":"[email protected]","UserDate":"Feb 22, 2012 7:01:24 PM","GearId":0,"GearValue":0,"Income":0,"Level":0,"MatchResult":0,"MatchType":0,"OfferId":0,"OpponentId":0,"Partners":0,"ExhibitionCount":0,"PowerRuns":0,"PowerServes":0,"PowerShots":0,"Seeds":0,"Energy":0,"Cash":0,"Stamina":0,"Strength":0,"SubLevel":0,"TotalEnergy":0,"TotalStamina":0,"TrainingId":0,"Agility":0,"UserId":0,"Age":0,"ActivityId":0,"gearIsGift":0}</user> 
</authenticateUser> 
</v:Body> 
</v:Envelope> 

我不知道为什么额外的属性像“id”和“c:root”被添加到authenticateUser中。 和i:type =“d:String”中的额外属性。 请有人给我一个很好的例子或教程,可以指导我创建一个像上面的请求,真的需要帮助谢谢。

+0

我假设你显示的第一个请求,你从soapUI得到它?你必须知道ksoap2的enveloppe看起来与发送的soap有点不同,比如说使用普通soap在netbeans中完成的java程序。这不是问题,两者都会得到相同的结果,它只是xml。您是否从您的请求中收到错误消息,或者您只是担心额外的附加属性? – shadesco 2012-02-23 03:33:07

+0

HI请连接我在这个聊天室..我有一些怀疑这就是为什么,https://chat.stackoverflow.com/rooms/146715/soap – 2017-06-15 05:17:44

回答

1

终于让它与KSOAP一起工作。这里是我使用的代码,也许它会帮助别人。

final SoapObject request = new SoapObject(AppConsts.NAMESPACE, 
       usecaseString); 
     request.addProperty(addPropertyString, propertyJsonString); 
     final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 
     final HttpTransportSE androidHttpTransport = new HttpTransportSE(
       AppConsts.URL); 
     androidHttpTransport.debug = true; 
     String soapAction = AppConsts.NAMESPACE + usecaseString; 

     try { 
      androidHttpTransport.call(soapAction, envelope); 
      SoapPrimitive resultSoapPrimitive; 
      resultSoapPrimitive = (SoapPrimitive) envelope.getResponse(); 
      if (resultSoapPrimitive != null) { 
       result = resultSoapPrimitive.toString(); 
       if (AppConsts.ENABLE_LOG) 
        Log.d(AppConsts.GAME_TITLE, "result json : " + result); 
      } else { 
       if (AppConsts.ENABLE_LOG) 
        Log.d(AppConsts.GAME_TITLE, "result json is NULL!!! "); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.e("static", "Exception in making call to server"); 
     } 

对于创建上述请求,我们需要将这三个参数传入代码。

AppConsts.NAMESPACE = "http://playinktennismasters.com" 
usecaseString = "authenticateUser" 
addPropertyString = "user" 
+0

wt是你的网址?一个.svc或.asmx? – Ronnie 2012-08-10 07:59:43

+0

我得到一个:InternalServiceFault – Ronnie 2012-08-10 08:48:00

+0

它是.asmx ... – Waqas 2012-08-10 09:12:47

5

我使用简单的HttpClient和Httppost, 简单的字符串用于请求Envelope。

 String temp = "<?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:Body>" 
      + "<authenticateUser xmlns=\"http://playinktennismasters.com/\">" 
      + "<user>%s</user>" + "</authenticateUser>" + "</soap:Body>" 
      + "</soap:Envelope>"; 
    ENVELOPE = String.format(temp, user); 

现在使用Method将创建post请求剩余参数,并将返回响应String。

public String CallWebService(String url, String soapAction, String envelope) { 
    final DefaultHttpClient httpClient = new DefaultHttpClient(); 
    // request parameters 
    HttpParams params = httpClient.getParams(); 
    HttpConnectionParams.setConnectionTimeout(params, 10000); 
    HttpConnectionParams.setSoTimeout(params, 15000); 
    // set parameter 
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true); 

    // POST the envelope 
    HttpPost httppost = new HttpPost(url); 
    // add headers 
    httppost.setHeader("soapaction", soapAction); 
    httppost.setHeader("Content-Type", "text/xml; charset=utf-8"); 

    String responseString = "Nothingggg"; 
    try { 

     // the entity holds the request 
     HttpEntity entity = new StringEntity(envelope); 
     httppost.setEntity(entity); 

     // Response handler 
     ResponseHandler<String> rh = new ResponseHandler<String>() { 
      // invoked when client receives response 
      public String handleResponse(HttpResponse response) 
        throws ClientProtocolException, IOException { 

       // get response entity 
       HttpEntity entity = response.getEntity(); 

       // read the response as byte array 
       StringBuffer out = new StringBuffer(); 
       byte[] b = EntityUtils.toByteArray(entity); 

       // write the response byte array to a string buffer 
       out.append(new String(b, 0, b.length)); 
       return out.toString(); 
      } 
     }; 

     responseString = httpClient.execute(httppost, rh); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.d("me","Exc : "+ e.toString()); 

    } 

    // close the connection 
    httpClient.getConnectionManager().shutdown(); 
    return responseString; 
} 
4

删除ID EC:根属性装饰设置为false:

envelope.setAddAdornments(false); 

删除我:type属性,对于SimpleTypes,设置隐式类型为true

envelope.implicitTypes = true; 

但是在使用ComplexTypes时,要删除“i:type”,您需要使用kso​​ap 3.0.0 RC1或更高版本。我现在正在使用3.0.0 RC2,但当它变得可用时,我会升级到稳定版3.0.0发行版。

+0

3年后,你救了我的一天!非常感谢! – 2015-07-07 14:58:53