最简单的方法是创建一个带有 XML 请求值的字符串。例如,您可以将此 XML 字符串请求值传递给以下函数以从服务器获取响应。
仅供参考,您可以通过在 HTTPPost 对象内设置实体值来传递请求,与以下示例相同。您也可以通过这种方式传递 JSON 请求值。
例如:
public HttpResponse postData(String strXML) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("web service URL");
try {
StringEntity strEntity = new StringEntity(strXML,HTTP.UTF_8);
strEntity.setContentType("text/xml");
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(strEntity); // here you can set request value.
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return response;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}