如何在android中使用KSoap 2

我刚刚遇到了ksoap2在Android应用程序中使用我自己的asp.netnetworking服务。 我发现互联网上的一些伟大的资源,我已经实现了我的web服务在android应用程序。

以下是我使用的webservice的响应:

HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?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> <CheckAuthenticationResponse xmlns="http://tempuri.org/"> <CheckAuthenticationResult>boolean</CheckAuthenticationResult> </CheckAuthenticationResponse> </soap:Body> </soap:Envelope> 

为了消费上述服务,我实现了以下代码:

 public static Boolean isAuthenticated(String UserName, String Password) { String NAMESPACE = "http://tempuri.org/"; String METHOD_NAME = "CheckAuthentication"; String SOAP_ACTION = "http://tempuri.org/CheckAuthentication"; String URL = "http://primehangout.com/primehangoutweb.asmx"; SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); PropertyInfo pi = new PropertyInfo(); pi.setName("UserId"); pi.setValue(UserName); pi.setType(String.class); Request.addProperty(pi); PropertyInfo pi2 = new PropertyInfo(); pi2.setName("Password"); pi2.setValue(Password); pi2.setType(String.class); Request.addProperty(pi2); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(Request); try { AndroidHttpTransport transp = new AndroidHttpTransport(URL); transp.call(SOAP_ACTION, envelope); SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); return Boolean.parseBoolean(result.toString()); } catch(Exception e) { } return false; } 

它工作正常..但现在我要去消费一个服务。 所需服务的请求格式如下:

 POST /primehangoutweb.asmx HTTP/1.1 Host: primehangout.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/GetComment" <?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> <AuthSoapHd xmlns="http://tempuri.org/"> <strUserName>string</strUserName> <strPassword>string</strPassword> </AuthSoapHd> </soap:Header> <soap:Body> <GetComment xmlns="http://tempuri.org/"> <UId>string</UId> <refID>int</refID> </GetComment> </soap:Body> </soap:Envelope> 

所需服务的回应如下:

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?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> <GetCommentResponse xmlns="http://tempuri.org/"> <GetCommentResult> <xsd:schema>schema</xsd:schema>xml</GetCommentResult> </GetCommentResponse> </soap:Body> </soap:Envelope> 

我已经使用XMLReader类在我以前的iPhone应用程序中使用相同的服务,但由于我是Android的新手,我需要你的帮助人。

🙂

感谢所有阅读我的文章!