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

一、发送xml数据:

public static void main(String[] args) throws Exception { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 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程序设计有所帮助。

更多相关文章

  1. Android(安卓)实现Json数据解析,并进行应用!
  2. Android中读取文本文件中内容的方法
  3. Android(安卓)使用jsoup 进行数据抓取
  4. Android网络数据开关用法简单示例
  5. Android使用Broadcast实现无序静态与动态广播功能
  6. Android(安卓)libmp3lame详解
  7. 从输入流中获取数据并以字节数组返回,这种输入流可以来自Android
  8. 【android】存储数组数据到SharedPreferences
  9. Android开发5:短信发送器

随机推荐

  1. XML Schema的一个简单的例子
  2. 使用XML与Asp实现交互的一个示例代码分享
  3. XML开发基础-XML树结构
  4. XML-Spy使XML编辑过程的详细介绍
  5. XML开发基础-XML验证代码分享
  6. xml文件正确性验证类的示例代码分析
  7. 详细介绍使用XML化的思维组织数据(图)
  8. XML开发基础-XML语法规则详解
  9. XmlDocument XML编码转换的示例代码分享
  10. XML开发基础-XML元素