SOAP信封响应错误:读取XMLStreamReader时出错

时间:2012-09-24 15:28:57

标签: java android web-services soap axis

我在下面有以下Android应用代码。我正在尝试通过HTTP连接到Web服务。 Web服务使用apache轴。但是我在响应中遇到错误“Error reading 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();
            }
        }
    }
}
}

3 个答案:

答案 0 :(得分:8)

您的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 );

另外,不要忘记使用webservice正在使用的适当命名空间来更改上面的命名空间(m)。以及您尝试调用的操作的操作名称( GetStockPrice )。另外不要忘记参数名称和类型。

2-您可以使用Apache Axis生成客户端并将该客户端与您的应用程序一起使用

有关详细信息和推荐的客户How to call a SOAP web service on Android

,请参阅此主题

答案 1 :(得分:2)

Shoukry K是对的。您已完成HTTP POST请求。这不是一个肥皂网络服务。如果您想知道如何拨打网络服务,请点击链接。 http://www.youtube.com/watch?v=v9EowBVgwSo。确保下载最新的ksoap jar文件。

答案 2 :(得分:1)

使用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 Helper Method

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