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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Ruthless   2021-03-20 我要评论
想了解Android发送xml数据给服务器的方法的相关内容吗,Ruthless在本文为您仔细讲解Android发送xml数据给服务器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,发送,xml数据,服务器,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们