如何在 Android 中使用 KSoap 2
我刚刚发现 ksoap2 在 Android 应用程序中使用我自己的 asp .net Web 服务。 我在互联网上发现了一些很棒的资源,并且我已经在 Android 应用程序中实现了我的网络服务。
以下是我使用的网络服务的响应:
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>
我在以前的 iPhone 应用程序中使用 XMLReader 类使用了相同的服务,但由于我是 android 新手,我需要你们的帮助。
:)
感谢大家阅读我的帖子!
I've just came across to ksoap2 for using my own asp .net webservices in android apps.
I've found few great resources over internet and I've implemented my webservice in android app.
Following is the webservice's response I consumed:
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>
For consuming the above service I implemented the following code:
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;
}
It's working fine..
But now I'm going to consume a service.
The required service's request format is as follow:
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>
And response of desired service is following:
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>
I've consumed the same services in my previous iPhone application using XMLReader classes but as I'm a newbie in android, I need your help guys.
:)
Thanks to all for reading my post!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了集成 KSoap 2,您必须将依赖项添加到 build.gradle 文件中。
修改AndroidManifest.xml
为了对互联网上公开的 Web 服务进行肥皂调用,需要添加额外的权限。
然后深入代码
For Integrating KSoap 2 You have to add dependency to your build.gradle file.
Modify
AndroidManifest.xml
In order to make soap calls on a web service exposed on the internet, additional permissions need to be added.
Then dive into code