分享

通过HttpClient请求webService

 昵称9379950 2012-03-26
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./2003/05/soap-envelope" xmlns:sam="http://user.service.">
   <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./2003/05/soap-envelope\" xmlns:sam=\"http://user.service.\">");
        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./2003/05/soap-envelope">
    <soapenv:Body>
        <ns:getUserInfoResponse xmlns:ns="http://user.service.">
            <ns:return>xxx</ns:return>
        </ns:getUserInfoResponse>
    </soapenv:Body>
</soapenv:Envelope>
其中,<ns:return>内的"xxx"可以是json数据,android只需解析标签<ns:return>里的json数据即可。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多