打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过HttpClient请求webService
1

通过HttpClient请求webService

标签: #httpclient #webservice #android |  发布时间:2011-11-09

通过HttpClient请求webService
由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求,获取webService数据。

服务端使用的webService框架是axis2,请求数据之前,要封装一个xml格式,再通过post请求,获取服务端数据。
请求的xml格式如下所示:
1
2
3
4
5
6
7
8
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sam="http://user.service.xxx.com">
   <soap:Header/>
   <soap:Body>
      <sam:getUserInfo>
     <sam:userName>sunlightcs</sam:userName>
      </sam:getUserInfo>
   </soap:Body>
</soap:Envelope>
其中:getUserInfo是方法名,userName是参数名,当然,还可以加多个参数。


下面的代码是向webService发送请求,获取数据,返回的数据是xml形式的,android只要解析xml数据,就可以获得想要的数据了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
  
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
  
  
public class ClientTest {
  
    public static void main(String[] args) {
        ClientTest.httpClientPost();
    }
      
    private static void httpClientPost() {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8080/xxx/services/userService");
          
        try {
            ContentProducer cp = new ContentProducer() {
                public void writeTo(OutputStream outstream) throws IOException {
                    Writer writer = new OutputStreamWriter(outstream, "UTF-8");
                      
                    /**
                     * 获取请求的xml格式数据
                     */
                    String requestXml = getRequestXml();
                    writer.write(requestXml);
                    writer.flush();
                }
            };
  
            post.setEntity(new EntityTemplate(cp));
            HttpResponse response = client.execute(post);
              
        //打印返回的xml数据
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
      
      
    private static String getRequestXml(){
        StringBuilder sb = new StringBuilder("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">");
        sb.append("<soap:Header/>");
        sb.append("<soap:Body>");
        sb.append("<sam:getUserInfo>");
        sb.append("<sam:userName>sunlightcs</sam:userName>");
        sb.append("</sam:getUserInfo>");
        sb.append("</soap:Body>");
        sb.append("</soap:Envelope>");
          
        return sb.toString();
    }
  
}

返回的数据格式如下:
1
2
3
4
5
6
7
8
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ns:getUserInfoResponse xmlns:ns="http://user.service.xxx.com">
            <ns:return>xxx</ns:return>
        </ns:getUserInfoResponse>
    </soapenv:Body>
</soapenv:Envelope>
其中,<ns:return>内的"xxx"可以是json数据,android只需解析标签<ns:return>里的json数据即可。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Apache JMeter 测试webservice接口
ksoap2-android的简单使用
Java 使用httpclient Post与cxf 发布的Webservice通信
JAVA 调用Web Service的方法 - 轻松 的博客 - 博客园
Java调用.net的WebService - 阿善的日志 - 网易博客
JAVA6开发WebService (三)——几个概念
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服