Android中无法按要求生成ksoap2请求,求协助生成指定SOAP XML
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/andfetchAllDoctorSpecialityWise). - Optional Parameters: The
passwordis marked as optional, so we only add it if it's not null/empty. - SOAP Version: We use
SoapEnvelope.VER11because 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

