zoukankan      html  css  js  c++  java
  • Android之通过HTTP协议向服务器发送XML数据

           可以通过装载或读取一个XML文件,得到其数据,然后把得到的数据当成实体,通过HTTP协议用输出流发送给服务器,在服务器端通过获取输入流获取相关数据,这样就是实现了向服务器发送XML数据。如下:
     
    客户端:

     public void sendXmlTest() throws Exception{
    //通过类装载器装载XML资源
    InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("test.xml");
    byte[] xml=StreamTool.read(inputStream);

    String path="http://172.22.35.112:8080/videonews/GetXmlInfo";
    URL url=new URL(path);
    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setConnectTimeout(5000);
    conn.setDoOutput(true);

    //设置HTTP请求的头字段
        conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //内容类型
        conn.setRequestProperty("Content-Length", String.valueOf(xml.length));  //实体内容的长度

        conn.getOutputStream().write(xml);  //通过输出流把数据写到服务器
    if(conn.getResponseCode()==200){
    System.out.println("发送成功!");
    }else{
    System.out.println("发送失败!");
    }
    }

     
     
    服务器端:
    1. byte[] xml=StreamTool.read(request.getInputStream());  //获得输出流
    2. System.out.println(new String(xml,"UTF-8"));
  • 相关阅读:
    如何获取汉字对应的拼音
    php each()函数和list()函数
    php list()函数
    addslashes给预定义字符前面加上反斜杠
    array_filter() 过滤数组中的空白元素
    用.htaccess文件实现URL重写
    xml中实体引用
    onsubmit阻止表单提交
    php获取当前文件绝对路径
    array_merge() 函数的用法
  • 原文地址:https://www.cnblogs.com/allenzheng/p/3062610.html
Copyright © 2011-2022 走看看