问题
I want to build a soap request using KSOAP2 for android application. how to create a request for the given below soap request.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:glob="http://sap.com/xi/SAPGlobal20/Global"
xmlns:yrt="http://0021611689-one-off.sap.com/YRTWIVFXY_"
xmlns:ytk="http://0021611689-one-off.sap.com/YTK2PLNNY_"
xmlns:glob1="http://sap.com/xi/AP/Globalization">`
<soapenv:Header/>
<soapenv:Body>
<glob:CustomerBundleMaintainRequest_sync_V1>
<BasicMessageHeader>
</BasicMessageHeader>
<Customer>
<InternalID>234569</InternalID>
<!--Optional:-->
<CategoryCode>1</CategoryCode>
<CustomerIndicator>true</CustomerIndicator>
<!--Optional:-->
<LifeCycleStatusCode>2</LifeCycleStatusCode>
<!--Optional:-->
</Customer>
</glob:CustomerBundleMaintainRequest_sync_V1>
</soapenv:Body>
</soapenv:Envelope>
I have written the Android code and tried to build the request it shows an error that Soapfault message error. Find below the android code
public class CreateCustomer {
public void createCustomerAccount() throws IOException {
SoapObject soapObject = new SoapObject(NAME_SPACE,METHOD_NAME);
soapObject.addProperty("InternalID","98765");
soapObject.addProperty("CategoryCode","1");
soapObject.addProperty("CustomerIndicator","true");
soapObject.addProperty("LifeCycleStatusCode","2");
}
回答1:
For Building soap request ..
public String sendSoapRequest(Context c) throws Exception {
String finalString = "Paste your whole request through which you can send request from browser sucessfully";
Log.i("TAG", "*********************** FinalString Before "
+ FinalString);
// send SOAP request
InputStream resInputStream = sendRequest(FinalString);
// create the response SOAP envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// process SOAP response
parseResponse(resInputStream, envelope);
Object bodyIn = envelope.bodyIn;
SoapObject RequestSOAP = (SoapObject) envelope.bodyIn;
String response = RequestSOAP.getProperty(0).toString();
if (bodyIn instanceof SoapFault) {
throw (SoapFault) bodyIn;
}
return response.toString();
}
calling sendRequest ..
private InputStream sendRequest(String requestContent) throws Exception {
// initialize HTTP post
HttpPost httpPost = null;
try {
httpPost = new HttpPost(PostURL);
httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.addHeader("SOAPAction", "Your Soap Action");
} catch (Throwable e) {
Log.e("LOG_TAG", "Error initializing HTTP post for SOAP request", e);
// throw e;
}
// load content to be sent
try {
HttpEntity postEntity = new StringEntity(requestContent);
httpPost.setEntity(postEntity);
} catch (UnsupportedEncodingException e) {
Log.e("LOG_TAG",
"Unsupported ensoding of content for SOAP request", e);
throw e;
}
// send request
HttpResponse httpResponse = null;
HttpClient httpClient = new DefaultHttpClient();
try {
httpResponse = httpClient.execute(httpPost);
} catch (Throwable e) {
Log.e("LOG_TAG", "Error sending SOAP request", e);
// throw e;
}
// get SOAP response
try {
// get response code
int responseStatusCode = httpResponse.getStatusLine()
.getStatusCode();
// if the response code is not 200 - OK, or 500 - Internal error,
// then communication error occurred
if (responseStatusCode != 200 && responseStatusCode != 500) {
String errorMsg = "Got SOAP response code "
+ responseStatusCode + " "
+ httpResponse.getStatusLine().getReasonPhrase();
// ...
}
// get the response content
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
return is;
} catch (Throwable e) {
Log.e("LOG_TAG", "Error getting SOAP response", e);
// throw e;
}
return null;
}
call parseResponse
/**
* Parses the input stream from the response into SoapEnvelope object.
*/
private void parseResponse(InputStream is, SoapEnvelope envelope)
throws Exception {
try {
XmlPullParser xp = new KXmlParser();
xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
xp.setInput(is, "UTF-8");
envelope.parse(xp);
} catch (Throwable e) {
Log.e("LOG_TAG", "Error reading/parsing SOAP response", e);
}
}
call SendOrderDetails...
private class SendOrderDetails extends AsyncTask<String, CartViewItemsBean, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg) {
String fdfd = "";
try {
fdfd = sendSoapRequest(getActivity());
} catch (Exception e) {
e.printStackTrace();
}
return fdfd;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("transactionresponse", result);
if (!result.equalsIgnoreCase("")) {
try {
helpher.deleteTotalRecord();
String ffsd = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + result.toString();
XmlToJson xmlToJson = new XmlToJson.Builder(ffsd.trim()).build();
JSONObject jsonObject = xmlToJson.toJson();
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
}
And you have to use library XmlToJson finally you have to call this using
new SendOrderDetails().execute();
来源:https://stackoverflow.com/questions/51255957/soap-request-creation-using-ksoap2-for-multilevel-tags