加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – SOAP Envelope响应错误:读取XMLStreamReader时出错

发布时间:2020-12-14 05:53:26 所属栏目:Java 来源:网络整理
导读:我在下面有以下 Android应用程序代码.我正在尝试通过HTTP连接到Web服务. Web服务使用apache轴.但是我在响应中遇到错误“Error reading XMLStreamReader”.我真的被卡住了,不知道我能做些什么.可能是因为在服务器端和客户端使用了不同版本的HTTP客户端和SOAP
我在下面有以下 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();
            }
        }
    }
}
}

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读