zoukankan      html  css  js  c++  java
  • android学习之 向网络中发送 XML格式数据

    客户端:android 

    步骤1 先对一个xml进行数据解析为字节数组(byte[])  然后再通过HttpURLConnection 进行配置发送 关键代码如下

    byte[] data=NetWorkTool.getUrlData(将要解析的url路径); 
                HttpURLConnection conn=(HttpURLConnection) new URL("将要请求的url路径").openConnection();
                conn.setConnectTimeout(5000);  // 设置连接超时时间
                conn.setRequestMethod("POST"); // 设置请求方式
                conn.setDoOutput(true); //设置允许发送数据
                conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");  //设置内容类型
                conn.setRequestProperty("Content-Length", String.valueOf(data.length)); //数据长度
                conn.getOutputStream().write(data);  //写入数据到缓冲区 
                if(conn.getResponseCode() == 200){ //  这里才是真正发送数据
                    
                    System.out.println("发送成功");
                }else{
                    System.out.println("发送失败");
                }

    服务端解析传递过来的xml数据  :servlet

    InputStream input=req.getInputStream();
            byte[] data=StreamTool.read(input);
            String str=new String(data,"UTF-8");
            System.out.println(str);
  • 相关阅读:
    [转]难过的时候看看,也许会豁然开朗
    热爱生活
    [转]MTOM 编码
    11/16
    11/10 The Day Before Single's Day
    About working overtime
    hehe
    The First Blog
    配置MapServer出现的一些问题及解决办法
    Ubuntu 系统下终端快捷键设置
  • 原文地址:https://www.cnblogs.com/xiaxiayige/p/3442446.html
Copyright © 2011-2022 走看看