SOAP 信封响应错误:读取 XMLStreamReader 时出错
Posted
技术标签:
【中文标题】SOAP 信封响应错误:读取 XMLStreamReader 时出错【英文标题】:SOAP Envelope Response Error: Error reading XMLStreamReader 【发布时间】:2012-09-16 02:19:23 【问题描述】:我在下面有以下 android 应用代码。我正在尝试通过 HTTP 连接到 Web 服务。 Web 服务使用 apache 轴。但是,我在响应中遇到了错误“读取 XMLStreamReader 时出错”。我真的被困住了,不知道我能做什么。会不会是服务器端和客户端使用了不同版本的 HTTP 客户端和 SOAP?对此的任何帮助将不胜感激。 Web 服务非常简单:sayHello 方法显示 arg0=some_string 中给出的参数
public class MainActivity extends Activity
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
BufferedReader in = null;
try
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(
"http://10.0.0.63:8080/archibus/cxf/HelloWorld/sayHello");
request.addHeader("Content-Type", "text/xml");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("arg0", "testing"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
sb.append(line + NL);
in.close();
String page = sb.toString();
// Log.i(tag, page);
System.out.println(page);
catch (Exception e)
e.printStackTrace();
finally
if (in != null)
try
in.close();
catch (IOException e)
e.printStackTrace();
【问题讨论】:
lalit3686.blogspot.in/2012/06/… 【参考方案1】:您的 WebService 请求构造不正确。您实际上是在创建表单请求,而不是实际的 SOAP 请求。
SOAP 请求是具有信封和正文的 XML 文档,请参见此处的示例 SOAP Message Example on Wikipedia。
您在此处实际执行的是模拟提交表单的标准 HTTP 调用,而不是 SOAP 调用。
这里有两种解决方案:
1- 您可以通过手动创建 XML 文档并提交它来模拟 SOAP 客户端的行为。 除了将正确的 XML 文档设置为请求正文之外,不要忘记设置正确的标头:SOAPAction、Content-Type 和 Content-Length
RequestEntity requestEntity = new StringRequestEntity("<?xml version=\"1.0\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header></soap:Header><soap:Body><m:GetStockPrice xmlns:m=\"http://www.example.org/stock\"><m:StockName>IBM</m:StockName></m:GetStockPrice></soap:Body></soap:Envelope>");
post.setRequestEntity(requestEntity );
也不要忘记将上面的名称空间 (m) 更改为您的 Web 服务正在使用的正确名称空间。以及您尝试调用的操作的操作名称 (GetStockPrice )。也不要忘记参数名称和类型。
2- 您可以使用 Apache Axis 生成客户端并将该客户端与您的应用程序一起使用
有关更多信息和推荐的客户How to call a SOAP web service on Android,请参阅此主题
【讨论】:
【参考方案2】:Shoukry K 是对的。您已经完成了一个 HTTP POST 请求。这不是一个肥皂网络服务。如果您想知道如何调用网络服务,请点击链接。 http://www.youtube.com/watch?v=v9EowBVgwSo。确保下载最新的 ksoap jar 文件。
【讨论】:
【参考方案3】:使用 K-SOAP for Android 的示例代码。
private void sendSOAPmsg(DamageAssessmentFormPojo pojo) throws IOException, XmlPullParserException, SoapFault
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_REPORT);
request.addProperty("xmlBytes", Util.getSoapBase64String(pojo));
request.addProperty("fileName", IO.DefaultReportName);
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_REPORT);
if (response.toString().equalsIgnoreCase("true"))
Logger.logInfo("REPORT SENT SUCCESSFULLY", "WEB-SERVICE");
pojo.setReportSent(true);
IO.writeObject(pojo.getReportsFolderPath() + IO.DefaultReportName, pojo);
else
Logger.logInfo("REPORT SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
private SoapPrimitive sendSOAPEnvelope(SoapObject request, String soapAction) throws IOException, XmlPullParserException, SoapFault
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(WEBSERVICE.URL);
androidHttpTransport.call(soapAction, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response;
private boolean sendSOAPimage(String strImg, File pFile) throws IOException, XmlPullParserException
boolean result = false;
if (strImg != null)
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_IMAGE);
request.addProperty("imageBytes", strImg);
request.addProperty("fileName", pFile.getName());
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_MAGE);
if (response.toString().equalsIgnoreCase("true"))
result = true;
Logger.logInfo("IMAGE SENT SUCCESSFULLY", "WEB-SERVICE");
else
Logger.logInfo("IMAGE SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
return result;
//--------Util 辅助方法
public static String getSoapBase64String(DamageAssessmentFormPojo pojo)
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag("", XMLTags.TAG_ROD);
xmlSerializer.startTag("", XMLTags.TAG_ORDER);
xmlSerializer.startTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.text(pojo.getCheckedSeverity_Complexity());
xmlSerializer.endTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.startTag("", XMLTags.TAG_DAMAGE_TYPE);
StringBuilder builder = new StringBuilder();
for (String str : pojo.getCheckedDamageTypes())
builder.append(str + " , ");
xmlSerializer.text(builder.toString());
xmlSerializer.endTag("", XMLTags.TAG_DAMAGE_TYPE);
xmlSerializer.startTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.text(pojo.getCheckedEnvImpact());
xmlSerializer.endTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.startTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.text(pojo.getEnvImpactComments());
xmlSerializer.endTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.text(pojo.getCheckedTravelCond());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.text(pojo.getTravCondComments());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.text(pojo.getDateTime());
xmlSerializer.endTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.endTag("", "Order");
xmlSerializer.endTag("", "ROD");
xmlSerializer.endDocument();
catch (IllegalArgumentException e)
Logger.logException(e);
catch (IllegalStateException e)
Logger.logException(e);
catch (IOException e)
Logger.logException(e);
return Base64.encode(writer.toString().getBytes());
【讨论】:
以上是关于SOAP 信封响应错误:读取 XMLStreamReader 时出错的主要内容,如果未能解决你的问题,请参考以下文章
KSOAP 生成的 SOAP 请求不会导致任何错误但无法正常工作