android 通过HTTP协议发送XML数据并调用

Web Service(WEB服务)能够快捷和方便地综合并结合各种系统、商务和任何应用平台。新出现的 Web Services 标准: SOAP、WSDL 和 UDDI 能够使任何系统和系统之间的应用变为更加方便和廉价。

 

Web服务(Web Services)和Service—Oriented Architecture作为实现分布式系统和履行公司内部、公司之间的应用整合的技术和架构出现。SOA和Web服务的体系结构是两个不同层面的问题,前者是概念模式,面向商业应用;后者则是实现模式,面向技术框架。

 

面向服务的体系结构所表示的是一个概念上的模型,在这个模型中,松散耦合的应用在网络上被描述、发布和调用。而Web服务则是一组由协议构成的协议栈所定义的框架结构,它定义了在不同的系统之间通信松散耦合的编程框架。也可以认为,Web服务体系结构实际上是面向服务的体系结构的一个特定实现;面向服务的体系结构作为一个概念上的模型,将网络、传输协议以及安全等具体的细节都遗留给特定的实现。Web服务中的SOAP,WSDL等都是具体实现细节的标准。

1、发送xml数据给服务器 ,并非以请求参数方式发送:

http://192.168.1.10:8080/video/manage.do?xml=<xml>....</xml>

 

服务器端代码:VideoManageAction.java

[html]  view plain copy
  1. public ActionForward getXML(ActionMapping mapping, ActionForm form,  
  2.   
  3.            HttpServletRequest request, HttpServletResponse response)  
  4.   
  5.            throws Exception {  
  6.   
  7.        InputStream inStream = request.getInputStream();  
  8.   
  9.        byte[] data = StreamTool.readInputStream(inStream);  
  10.   
  11.        String xml = new String(data, "UTF-8");  
  12.   
  13.        System.out.println(xml);  
  14.   
  15.        return mapping.findForward("result");  
  16.   
  17.     }  
  18.   
  19.   
  20.   
  21. J2SE编写客户端向web服务器发生xml数据  
  22.   
  23. public static void main(String[] args) throws Exception {  
  24.   
  25.     String path="http://192.168.1.100:8080/videoweb/video/manage.do?method=getXML";  
  26.   
  27. String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><persons><person id=\"23\"><name>中国</name><age>30</age></person></persons>";  
  28.   
  29.          
  30.   
  31.     byte[] data = xml.getBytes("UTF-8");//得到了xml的实体数据  
  32.   
  33.     URL url = new URL(path);  
  34.   
  35.     HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  36.   
  37.     conn.setConnectTimeout(5000);  
  38.   
  39.     conn.setRequestMethod("POST");  
  40.   
  41.     conn.setDoOutput(true);  
  42.   
  43.     conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");  
  44.   
  45.     conn.setRequestProperty("Content-Length",  
  46.   
  47. String.valueOf(data.length));  
  48.   
  49.          
  50.   
  51.     OutputStream outStream =  conn.getOutputStream();  
  52.   
  53.     outStream.write(data);  
  54.   
  55.     outStream.flush();  
  56.   
  57.     outStream.close();  
  58.   
  59.     System.out.println(conn.getResponseCode());  
  60.   
  61. }  
  62.   
  63. 2、发送SOAP数据给服务器调用webservice,实现手机号归属地查询  
  64. 访问网址www.webxml.com.cn ,浏览各种web service,打开手机号码归属地查询并进行解释。  
  65.   
  66.    
  67.   
  68. 实验步骤:  
  69.   
  70. 创建android工程MobileBelong,设置网络访问权限。  
  71.   
  72.    
  73.   
  74. 资源  
  75.   
  76.  <string name="hello">Hello World, MainActivity!</string>  
  77.   
  78.     <string name="app_name">手机号归属地查询</string>  
  79.   
  80.     <string name="mobile">手机号</string>  
  81.   
  82.     <string name="button">查询</string>  
  83.   
  84.     <string name="error">网络连接失败</string>  
  85.   
  86.  <TextView  
  87.   
  88.         android:layout_width="fill_parent"  
  89.   
  90.         android:layout_height="wrap_content"  
  91.   
  92.  android:text="@string/mobile" />  
  93.   
  94.    
  95.   
  96.     <EditText  
  97.   
  98.         android:id="@+id/mobile"  
  99.   
  100.         android:layout_width="fill_parent"  
  101.   
  102.         android:layout_height="wrap_content"  
  103.   
  104.         android:text="13472283596" />  
  105.   
  106.    
  107.   
  108.     <Button  
  109.   
  110.         android:id="@+id/button"  
  111.   
  112.         android:layout_width="wrap_content"  
  113.   
  114.         android:layout_height="wrap_content"  
  115.   
  116.         android:text="@string/button" />  
  117.   
  118.    
  119.   
  120.     <TextView  
  121.   
  122.         android:id="@+id/result"  
  123.   
  124.         android:layout_width="fill_parent"  
  125.   
  126.         android:layout_height="wrap_content" />  
  127.   
  128. 在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下  
  129.   
  130. <?xml version="1.0" encoding="utf-8"?>  
  131.   
  132. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  
  133.   
  134.   <soap12:Body>  
  135.   
  136.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">  
  137.   
  138.       <mobileCode>$mobile</mobileCode>  
  139.   
  140.       <userID></userID>  
  141.   
  142.     </getMobileCodeInfo>  
  143.   
  144.   </soap12:Body>  
  145.   
  146. </soap12:Envelope>  
  147.   
  148.   
  149.   
  150. 业务类:MobileService  
  151.   
  152. 注意访问目标地址是:  
  153.   
  154. http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx  
  155.   
  156. 可以有协议中得到。  
  157.   
  158. package cn.class3g.service;  
  159.   
  160. …  
  161.   
  162. public class MobileService {  
  163.   
  164.    
  165.   
  166. public static String getMobileAddress(String mobile) throws Exception {  
  167.   
  168. InputStream inStream = MobileService.class.getClassLoader()  
  169.   
  170.               .getResourceAsStream("mobilesoap.xml");  
  171.   
  172.        byte[] data = StreamTool.readInputStream(inStream);  
  173.   
  174.        String xml = new String(data);  
  175.   
  176.        String soap = xml.replaceAll("\\$mobile", mobile);  
  177.   
  178.    
  179.   
  180.        /**  
  181.   
  182.         * 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile  
  183.   
  184.         * 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;  
  185.   
  186.         */  
  187.   
  188.        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  189.   
  190.        data = soap.getBytes();// 得到了xml的实体数据  
  191.   
  192.        URL url = new URL(path);  
  193.   
  194.        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  195.   
  196.        conn.setConnectTimeout(5 * 1000);  
  197.   
  198.        conn.setRequestMethod("POST");  
  199.   
  200.        conn.setDoOutput(true);  
  201.   
  202.        conn.setRequestProperty("Content-Type",  
  203.   
  204.               "application/soap+xml; charset=utf-8");  
  205.   
  206.        conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  207.   
  208.        OutputStream outStream = conn.getOutputStream();  
  209.   
  210.        outStream.write(data);  
  211.   
  212.        outStream.flush();  
  213.   
  214.        outStream.close();  
  215.   
  216.        if (conn.getResponseCode() == 200) {  
  217.   
  218.            InputStream responseStream = conn.getInputStream();  
  219.   
  220.            return parseXML(responseStream);  
  221.   
  222.        }  
  223.   
  224.        return null;  
  225.   
  226.     }  
  227.   
  228.    
  229.   
  230.     /**  
  231.   
  232.      * 解析返回xml数据  
  233.   
  234.      *   
  235.   
  236.      * @param responseStream  
  237.   
  238.      * @return  
  239.   
  240.      * @throws Exception  
  241.   
  242.      */  
  243.   
  244.     private static String parseXML(InputStream responseStream) throws Exception {  
  245.   
  246.   
  247.   
  248. XmlPullParser parser = Xml.newPullParser();  
  249.   
  250.        parser.setInput(responseStream, "UTF-8");  
  251.   
  252.        int event = parser.getEventType();  
  253.   
  254.        while (event != XmlPullParser.END_DOCUMENT) {  
  255.   
  256.            switch (event) {  
  257.   
  258.            case XmlPullParser.START_TAG:  
  259.   
  260.               if ("getMobileCodeInfoResult".equals(parser.getName())) {  
  261.   
  262.                   return parser.nextText();  
  263.   
  264.               }  
  265.   
  266.               break;  
  267.   
  268.            }  
  269.   
  270.            event = parser.next();  
  271.   
  272.        }  
  273.   
  274.        return null;  
  275.   
  276.     }  
  277.   
  278. }  


工具类StreamTool

[html]  view plain copy
  1. package cn.class3g.utils;  
  2.   
  3. …  
  4.   
  5. public class StreamTool {  
  6.   
  7.     /**  
  8.   
  9.      * 从输入流读取数据  
  10.   
  11.      * @param inStream  
  12.   
  13.      * @return  
  14.   
  15.      * @throws Exception  
  16.   
  17.      */  
  18.   
  19.     public static byte[] readInputStream(InputStream inStream) throws Exception{  
  20.   
  21.        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  22.   
  23.        byte[] buffer = new byte[1024];  
  24.   
  25.        int len = 0;  
  26.   
  27.        while( (len = inStream.read(buffer)) !=-1 ){  
  28.   
  29.            outSteam.write(buffer, 0, len);  
  30.   
  31.        }  
  32.   
  33.        outSteam.close();  
  34.   
  35.        inStream.close();  
  36.   
  37.        return outSteam.toByteArray();  
  38.   
  39.     }  
  40.   
  41. }  


Activity类MobileBelongActivity

[html]  view plain copy
  1. package cn.class3g.mobile;  
  2.   
  3. public class MobileBelongActivity extends Activity {  
  4.   
  5.    
  6.   
  7.     private static final String TAG = "MainActivity";  
  8.   
  9.     private EditText mobileText;  
  10.   
  11.     private TextView resultView;  
  12.   
  13.    
  14.   
  15.     @Override  
  16.   
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.   
  19.        super.onCreate(savedInstanceState);  
  20.   
  21.        setContentView(R.layout.main);  
  22.   
  23.    
  24.   
  25.        mobileText = (EditText) this.findViewById(R.id.mobile);  
  26.   
  27.        resultView = (TextView) this.findViewById(R.id.result);  
  28.   
  29.        Button button = (Button) this.findViewById(R.id.button);  
  30.   
  31.        button.setOnClickListener(new View.OnClickListener() {  
  32.   
  33.            @Override  
  34.   
  35.            public void onClick(View v) {  
  36.   
  37.               String mobile = mobileText.getText().toString();  
  38.   
  39.               try {  
  40.   
  41.                   String address = MobileService.getMobileAddress(mobile);  
  42.   
  43.                   resultView.setText(address);  
  44.   
  45.               } catch (Exception e) {  
  46.   
  47.                   Log.e(TAG, e.toString());  
  48.   
  49.                   Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();  
  50.   
  51.               }  
  52.   
  53.            }  
  54.   
  55.        });  
  56.   
  57.     }  
  58.   
  59. }  
  60. 启动服务器,运行客户端进行测试


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值