Web Service调用资料
来源:互联网 发布:C语言for( ; ; )可省略 编辑:程序博客网 时间:2024/05/18 16:37
先用随便什么东西做出个 Web Service,假设得到的Web Service地址如下:
http://192.168.0.100/android/WebService1.asmx
而这个 Web Service 中,提供了一个 Sum 函数,其实现如下:
public int Sum(int a, int b) {
return a + b;
}
则在 Android 中,可以按以下方式调用
public void callWS() throws ParserConfigurationException {
final String SERVER_URL = "http://192.168.0.100/android/WebService1.asmx/Sum";
HttpPost request = new HttpPost(SERVER_URL);
List params = new ArrayList();
params.add(new BasicNameValuePair("A", "1"));
params.add(new BasicNameValuePair("B", "2"));
try {
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
String result = "";
if (httpResponse.getStatusLine().getStatusCode() != 404) {
result = EntityUtils.toString(httpResponse.getEntity());
}
new AlertDialog.Builder(this).setTitle("Hint").setMessage(result)
.setPositiveButton("OK", null).show();
} catch (Exception e) {
new AlertDialog.Builder(this).setTitle("Hint").setMessage(
e.getMessage()).setPositiveButton("OK", null).show();
}
}
以上方法可以正确的得到 Web Service 的返回值,但是那是一个完整的 xml,需要进一步解析从而得到想要的结果
解析的方法如下所示:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(result.getBytes("UTF-8"));
Document dom = builder.parse(is);
String res = dom.getDocumentElement().getChildNodes().item(0).getNodeValue();
http://192.168.0.100/android/WebService1.asmx
而这个 Web Service 中,提供了一个 Sum 函数,其实现如下:
public int Sum(int a, int b) {
return a + b;
}
则在 Android 中,可以按以下方式调用
public void callWS() throws ParserConfigurationException {
final String SERVER_URL = "http://192.168.0.100/android/WebService1.asmx/Sum";
HttpPost request = new HttpPost(SERVER_URL);
List params = new ArrayList();
params.add(new BasicNameValuePair("A", "1"));
params.add(new BasicNameValuePair("B", "2"));
try {
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
String result = "";
if (httpResponse.getStatusLine().getStatusCode() != 404) {
result = EntityUtils.toString(httpResponse.getEntity());
}
new AlertDialog.Builder(this).setTitle("Hint").setMessage(result)
.setPositiveButton("OK", null).show();
} catch (Exception e) {
new AlertDialog.Builder(this).setTitle("Hint").setMessage(
e.getMessage()).setPositiveButton("OK", null).show();
}
}
以上方法可以正确的得到 Web Service 的返回值,但是那是一个完整的 xml,需要进一步解析从而得到想要的结果
解析的方法如下所示:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(result.getBytes("UTF-8"));
Document dom = builder.parse(is);
String res = dom.getDocumentElement().getChildNodes().item(0).getNodeValue();
- Web Service调用资料
- web service 资料集锦。
- web service 百度百科资料
- 动态调用Web Service
- js调用web service
- Web Service 异步调用
- 调用 Web Service 示例
- ASP 调用 web service
- 异步调用Web Service
- 动态调用web service
- php调用web service
- 动态调用Web Service
- java 调用 Web Service
- 异步调用Web Service
- JQuery调用Web Service
- Web Service 调用
- C#调用 Web Service
- Web Service创建、调用
- MySQL BOOL/BOOLEAN 与 TINYINT测试总结
- Ajax缓存问题的解决办法
- mysql select
- 数据库外键的使用以及优缺点
- C#中怎么判断一个数组中是否存在某个数组值
- Web Service调用资料
- Android开发之旅: Intents和Intent Filters(理论部分)
- object-c学习:@property (nonatomic,retain)中的nonatom和retain的意思
- C缺陷和陷阱
- 获取SQLLite省市区数据库中省市区数据的方法
- Android开发之旅: Intents和Intent Filters(实例部分)
- myql delete
- Linux管道通信
- Redis数据类型及操作