欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android发送xml数据给服务器的方法

程序员文章站 2023-11-27 18:46:28
本文实例讲述了android发送xml数据给服务器的方法。分享给大家供大家参考。具体如下: 一、发送xml数据: public static void main...

本文实例讲述了android发送xml数据给服务器的方法。分享给大家供大家参考。具体如下:

一、发送xml数据:

public static void main(string[] args) throws exception {
 string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><videos><video><title>中国</title></video></videos>";
 string path = http://localhost:8083/videoweb/video/manage.do?method=getxml ;
 byte[] entity = xml.getbytes("utf-8");
 httpurlconnection conn = (httpurlconnection) new url(path).openconnection();
 conn.setconnecttimeout(5000);
 conn.setrequestmethod("post");
 conn.setdooutput(true);
 //指定发送的内容类型为xml
 conn.setrequestproperty("content-type", "text/xml; charset=utf-8");
 conn.setrequestproperty("content-length", string.valueof(entity.length));
 outputstream outstream = conn.getoutputstream();
 outstream.write(entity);
 if(conn.getresponsecode() == 200){
  system.out.println("发送成功");
 }else{
  system.out.println("发送失败");
 }
}

二、接受xml数据:

public actionforward getxml(actionmapping mapping, actionform form,
  httpservletrequest request, httpservletresponse response)
  throws exception {
 inputstream instream = request.getinputstream();
 byte[] data = streamtool.read(instream);
 string xml = new string(data, "utf-8");
 system.out.println(xml);
 return mapping.findforward("result");
}

希望本文所述对大家的android程序设计有所帮助。