服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android发送xml数据给服务器的方法

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

2021-03-31 14:59Ruthless Android

这篇文章主要介绍了Android发送xml数据给服务器的方法,以实例形式较为详细的分析了Android发送XML数据及接收XML数据的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

一、发送xml数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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数据:

?
1
2
3
4
5
6
7
8
9
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程序设计有所帮助。

延伸 · 阅读

精彩推荐