gpt4 book ai didi

java - 使用 ksoap2 创建多级标签的 Soap 请求

转载 作者:行者123 更新时间:2023-12-02 11:02:08 26 4
gpt4 key购买 nike

我想使用 KSOAP2 为 Android 应用程序构建一个肥皂请求。如何为下面给出的肥皂请求创建请求。

<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>

我已经编写了 Android 代码并尝试构建请求,但它显示了 Soapfault 消息错误。找到下面的android代码

    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");

}

最佳答案

对于建筑肥皂请求..

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();

关于java - 使用 ksoap2 创建多级标签的 Soap 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51255957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com