java - 如何在安卓中使用 KSoap 2

标签 java android ksoap2 android-ksoap2

我刚刚接触到 ksoap2,因为它在 android 应用程序中使用了我自己的 asp .net web 服务。 我在 Internet 上发现了一些很棒的资源,并且我已经在 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 的新手,我需要你们的帮助。

:)

感谢大家阅读我的帖子!

最佳答案

对于集成 KSoap 2 你必须添加依赖到你的 build.gradle 文件。

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}

dependencies {
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.2'
 }

修改AndroidManifest.xml 为了在 Internet 上公开的 Web 服务上进行 soap 调用,需要添加额外的权限。

<uses-permission android:name="android.permission.INTERNET" />

然后深入研究代码

Thread thread = new Thread() {
            @Override
            public void run() {
                String SOAP_ACTION = "https://www.w3schools.com/xml/FahrenheitToCelsius";
                String METHOD_NAME = "FahrenheitToCelsius";
                String NAMESPACE = "https://www.w3schools.com/xml/";
                String URL = "https://www.w3schools.com/xml/tempconvert.asmx";

                //Initialize soap request
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                //Use this to add parameters
                request.addProperty("Fahrenheit", "10");

                //Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);

                try {
                    //Needed to make the internet call
                    HttpTransportSE transport = new HttpTransportSE(URL);
                    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
                    headerList.add(new HeaderProperty("Authorization", "Basic " + Base64.encode((username + ":" + password).getBytes())));

                    transport.call(SOAP_ACTION, envelope, headerList);
                    //Get the Response from the envelope body.
                    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
                } catch (Exception e) {
                    Log.e("TESTS", "KSOAP2", e);
                }
            }
        };

        thread.start();

关于java - 如何在安卓中使用 KSoap 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5155027/

相关文章:

java - 字符串不能包含两个 '%'

java - NoClassDefFoundError at Runtime on class inside .AAR in Android Studio

android - 使用 KSOAP 的 Web 服务

android - kSOAP2 双 ID 异常

java - Kotlin 中有关于访问修饰符的文档吗?

java - Dr. Java中的空指针异常

java - 调用 onMeasure 时,Android 自定义 View 的宽度始终为 0

java - 使用 Ksoap2 将日期发送到服务器

java - 带有 JRE 7 的 Eclipse Juno 会导致启动问题

java - 在 spring boot 中渲染 JSP 的问题