You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中无法按要求生成ksoap2请求,求协助生成指定SOAP XML

Generate SOAP Request with ksoap2 for Android

Hey there! Let's get that SOAP request sorted out using ksoap2. I'll walk you through each step with code examples based on the SoapUI XML you provided.

First, let's clarify the target SOAP request we need to generate (I've completed the truncated part for clarity):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
soapenv:Header/
soapenv:Body
<app:fetchAllDoctorSpecialityWise xmlns:app="http://app.shl.com/">
?

?
?
</app:fetchAllDoctorSpecialityWise>
</soapenv:Body>
</soapenv:Envelope>

Step 1: Add ksoap2 Dependency

First, make sure you have the ksoap2 library added to your Android project. Add this to your app-level build.gradle (Module level) dependencies block:

implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'

Sync your project to fetch the library.

Step 2: Write the Code to Generate the Request

Here's a complete code snippet to build and generate the SOAP XML. I've added comments to explain each part:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SoapRequestGenerator {

    // Constants extracted from your SoapUI XML
    private static final String NAMESPACE = "http://app.shl.com/";
    private static final String METHOD_NAME = "fetchAllDoctorSpecialityWise";
    private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;

    public static void generateSoapRequest(String userId, String password, String specialityId) {
        // 1. Create the SoapObject for the request body
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        // 2. Add required parameters
        request.addProperty("userid", userId);
        // Add password only if it's provided (since it's optional)
        if (password != null && !password.isEmpty()) {
            request.addProperty("password", password);
        }
        request.addProperty("specialityID", specialityId);

        // 3. Create the SOAP Envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        // Set the request as the body content
        envelope.setOutputSoapObject(request);
        // Set dotNet to false since this isn't a .NET web service
        envelope.dotNet = false;

        // Optional: If you need to inspect the generated XML, you can print it like this
        try {
            String xmlOutput = envelope.bodyOut.toString();
            System.out.println("Generated SOAP Request:\n" + xmlOutput);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // If you need to send the request to the server, uncomment the below part
        /*
        try {
            HttpTransportSE transport = new HttpTransportSE("YOUR_WEB_SERVICE_URL");
            transport.call(SOAP_ACTION, envelope);
            // Handle the response here if needed
        } catch (Exception e) {
            e.printStackTrace();
        }
        */
    }
}

Key Notes:

  • Namespace & Method Name: These must match exactly what's in your SoapUI request (http://app.shl.com/ and fetchAllDoctorSpecialityWise).
  • Optional Parameters: The password is marked as optional, so we only add it if it's not null/empty.
  • SOAP Version: We use SoapEnvelope.VER11 because your SoapUI request uses the SOAP 1.1 envelope namespace (http://schemas.xmlsoap.org/soap/envelope/).
  • Inspect the XML: The code includes a way to print the generated XML so you can verify it matches the SoapUI output.

To use this, just call generateSoapRequest("your_user_id", "your_password", "your_speciality_id") (pass null or empty string for password if you don't need it).

内容的提问来源于stack exchange,提问作者ronihasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:16:36