did*_*ido 7 java android axis soap web-services
我在下面有以下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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
您的WebService请求构造不正确.您实际上是在创建表单请求而不是实际的SOAP请求.
SOAP请求是一个XML文档,它有一个信封和一个正文,请参阅维基百科上的SOAP消息示例.
你在这里实际做的是标准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 );
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记使用webservice正在使用的适当命名空间来更改上面的命名空间(m).以及您尝试调用的操作的操作名称(GetStockPrice).另外不要忘记参数名称和类型.
2-您可以使用Apache Axis生成客户端并将该客户端与您的应用程序一起使用
有关更多信息,请参阅此主题和推荐的客户端如何在Android上调用SOAP Web服务
归档时间: |
|
查看次数: |
32066 次 |
最近记录: |