通过HttpClient请求webService

通过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">
2   <soap:Header/>
3   <soap:Body>
4      <sam:getUserInfo>
5     <sam:userName>sunlightcs</sam:userName>
6      </sam:getUserInfo>
7   </soap:Body>
8</soap:Envelope>
其中:getUserInfo是方法名,userName是参数名,当然,还可以加多个参数。
 
 
下面的代码是向webService发送请求,获取数据,返回的数据是xml形式的,android只要解析xml数据,就可以获得想要的数据了。 

01import java.io.IOException;
02import java.io.OutputStream;
03import java.io.OutputStreamWriter;
04import java.io.Writer;
05 
06import org.apache.http.HttpResponse;
07import org.apache.http.client.HttpClient;
08import org.apache.http.client.methods.HttpPost;
09import org.apache.http.entity.ContentProducer;
10import org.apache.http.entity.EntityTemplate;
11import org.apache.http.impl.client.DefaultHttpClient;
12import org.apache.http.util.EntityUtils;
13 
14 
15public class ClientTest {
16 
17    public static void main(String[] args) {
18        ClientTest.httpClientPost();
19    }
20     
21    private static void httpClientPost() {
22        HttpClient client = new DefaultHttpClient();
23        HttpPost post = newHttpPost("http://localhost:8080/xxx/services/userService");
24         
25        try {
26            ContentProducer cp = new ContentProducer() {
27                public void writeTo(OutputStream outstream) throwsIOException {
28                    Writer writer = new OutputStreamWriter(outstream,"UTF-8");
29                     
30                    /**
31                     * 获取请求的xml格式数据
32                     */
33                    String requestXml = getRequestXml();
34                    writer.write(requestXml);
35                    writer.flush();
36                }
37            };
38 
39            post.setEntity(new EntityTemplate(cp));
40            HttpResponse response = client.execute(post);
41             
42        //打印返回的xml数据
43            System.out.println(EntityUtils.toString(response.getEntity()));
44        catch (IOException e) {
45            e.printStackTrace();
46        }
47    }
48     
49     
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>");
59         
60        return sb.toString();
61    }
62 
63}

返回的数据格式如下: 
1<?xml version='1.0' encoding='UTF-8'?>
2<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
3    <soapenv:Body>
4        <ns:getUserInfoResponse xmlns:ns="http://user.service.xxx.com">
5            <ns:return>xxx</ns:return>
6        </ns:getUserInfoResponse>
7    </soapenv:Body>
8</soapenv:Envelope>
其中,<ns:return>内的"xxx"可以是json数据,android只需解析标签<ns:return>里的json数据即可。 
转载 http://www.juziku.com/wiki/3919.htm
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值