通过HttpClient请求webService
由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求,获取webService数据。
服务端使用的webService框架是axis2,请求数据之前,要封装一个xml格式,再通过post请求,获取服务端数据。
请求的xml格式如下所示:
1 |
< soap:Envelope xmlns:soap = "http://www.w3.org/2003/05/soap-envelope" xmlns:sam = "http://user.service.xxx.com" > |
5 |
< sam:userName >sunlightcs</ sam:userName > |
其中:getUserInfo是方法名,userName是参数名,当然,还可以加多个参数。
下面的代码是向webService发送请求,获取数据,返回的数据是xml形式的,android只要解析xml数据,就可以获得想要的数据了。
01 |
import java.io.IOException; |
02 |
import java.io.OutputStream; |
03 |
import java.io.OutputStreamWriter; |
04 |
import java.io.Writer; |
06 |
import org.apache.http.HttpResponse; |
07 |
import org.apache.http.client.HttpClient; |
08 |
import org.apache.http.client.methods.HttpPost; |
09 |
import org.apache.http.entity.ContentProducer; |
10 |
import org.apache.http.entity.EntityTemplate; |
11 |
import org.apache.http.impl.client.DefaultHttpClient; |
12 |
import org.apache.http.util.EntityUtils; |
15 |
public class ClientTest { |
17 |
public static void main(String[] args) { |
18 |
ClientTest.httpClientPost(); |
21 |
private static void httpClientPost() { |
22 |
HttpClient client = new DefaultHttpClient(); |
23 |
HttpPost post = new HttpPost( "http://localhost:8080/xxx/services/userService" ); |
26 |
ContentProducer cp = new ContentProducer() { |
27 |
public void writeTo(OutputStream outstream) throws IOException { |
28 |
Writer writer = new OutputStreamWriter(outstream, "UTF-8" ); |
33 |
String requestXml = getRequestXml(); |
34 |
writer.write(requestXml); |
39 |
post.setEntity( new EntityTemplate(cp)); |
40 |
HttpResponse response = client.execute(post); |
43 |
System.out.println(EntityUtils.toString(response.getEntity())); |
44 |
} catch (IOException e) { |
50 |
private static String getRequestXml(){ |
51 |
StringBuilder sb = new StringBuilder( "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">" ); |
52 |
sb.append( "<soap:Header/>" ); |
53 |
sb.append( "<soap:Body>" ); |
54 |
sb.append( "<sam:getUserInfo>" ); |
55 |
sb.append( "<sam:userName>sunlightcs</sam:userName>" ); |
56 |
sb.append( "</sam:getUserInfo>" ); |
57 |
sb.append( "</soap:Body>" ); |
58 |
sb.append( "</soap:Envelope>" ); |
返回的数据格式如下:
1 |
<? xml version = '1.0' encoding = 'UTF-8' ?> |
2 |
< soapenv:Envelope xmlns:soapenv = "http://www.w3.org/2003/05/soap-envelope" > |
4 |
< ns:getUserInfoResponse xmlns:ns = "http://user.service.xxx.com" > |
5 |
< ns:return >xxx</ ns:return > |
6 |
</ ns:getUserInfoResponse > |
其中,<ns:return>内的"xxx"可以是json数据,android只需解析标签<ns:return>里的json数据即可。
转载 http://www.juziku.com/wiki/3919.htm
- jquery+ajax跨域请求webservice
最近几天在学习webservice...在学习的时候便想到用ajax的方式去请求webservice.. 一直在测试..如果这个被请求的webservice和自己使用的是同一个端口号.则不用考虑那aj ...
- Node.js 使用 soap 模块请求 WebService 服务接口
项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求. 使用SOAP请求webservice服务的流程如下: 1.进入项目目录,安装 so ...
- httpclient请求方法
/** * httpclient请求方法 * @param url 请求地址 * @param paramMap 请求参数 * @param ent 编码格式 gbk.utf-8 * @return ...
- JQuery请求WebService返回数据的几种处理方式
打开自己的博客仔细浏览了一番,发现已经好久没有写博客了,由于最近一直比较忙碌懈怠了好多.默默反省三分钟.......言归正传,现在就对最近在学习webservice的过程中遇到的几种类型的问题中我的理 ...
- ajax请求webservice时抛出终止线程的异常
请求webservice中以下接口,会抛出异常 {"Message":"正在中止线程.","StackTrace":" 在 Sys ...
- HttpClient请求服务器代码优化版
HttpClient请求服务器代码优化版 首先,我在前面的两篇博文中介绍了在 Android中,除了使用java.net包下HttpUrlConnection的API访问HTTP服务之外,我们还可以换 ...
- C# 使用 HttpClient 调用 WebService 提示 NoSOAPAction
问题 在自行构造 HttpClient 请求 SOAP 接口之后,返回 500 错误,并且提示 NoSOAPAction 信息. 原因 造成这个问题的主要原因是因为缺少了 SOAPAction 标头, ...
- webserive学习记录6-页面请求webservice
前面都是通过JAVA代码访问webservice服务,下面将介绍通过javascript,jquery访问webservice服务并介绍过过servlet解决跨域问题的方法. 服务端 编写服务代码,解 ...
- C# HttpClient 请求认证、数据传输笔记
目录 一,授权认证 二,请求类型 三,数据传输 C# HttpClient 请求认证.数据传输笔记 一,授权认证 客户端请求服务器时,需要通过授权认证许可,方能获取服务器资源,目前比较常见的认证方式有 ...
随机推荐
- apache make
https://jingyan.baidu.com/article/7e4409533d7f0f2fc0e2ef91.html 1. apr apr-util http://archive.apach ...
- 剪贴板增强---Kawvin增强剪贴板_V2.0
#Persistent SetWorkingDir,%A_ScriptDir% ;设置工作目录 #MaxThreadsPerHotkey ;最大热键数量 #NoEnv ;#Warn #SingleIn ...
- Tomcat 容器的设计和实现
Tomcat 容器是对 Servlet 规范的实现,也称为 Servlet 引擎.在分析 Tomcat 容器的设计和实现之前,首先简单了解一下 Servlet 规范,弄清楚 Tomcat 究竟要实现什 ...
- https请求失败,解决方法
把请求头中 Content-Type 属性去掉就可以了
- 汇编_指令_FLAGS
标志名 标志 1 标志 0 OF (溢出标志) OV ...
- 内核程序开发 LED灯顺序点亮、顺序熄灭
根据实际考试情况,你要按顺序这么来干. 首先,把老师给的文件1.LED灯顺序点亮.顺序熄灭导入到虚拟机里你喜欢的目录下 进入此目录1.LED灯顺序点亮.顺序熄灭: 看到这三个文件,依次打开背过,老师出 ...
- 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂
C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...
- 学习笔记之Android
Android 开发专区 - 开源中国社区 http://www.oschina.net/android 探索 Android Studio | Android Studio https://deve ...
- [html][javascript]动态增删页面元素
<script type="text/javascript"> function append(event){ var myhref = document.create ...
- [转]oracle 12c 中的分页子句
转自:http://blog.itpub.net/271063/viewspace-1061279/ -- 连接数据库 创建测试用户-- Connected to Oracle Database 12 ...