Android发送xml数据给服务器的方法
本文实例讲述了Android发送xml数据给服务器的方法。分享给大家供大家参考。具体如下:
一、发送xml数据:
publicstaticvoidmain(String[]args)throwsException{
Stringxml="<?xmlversion=\"1.0\"encoding=\"UTF-8\"?><videos><video><title>中国</title></video></videos>";
Stringpath=http://localhost:8083/videoweb/video/manage.do?method=getXML;
byte[]entity=xml.getBytes("UTF-8");
HttpURLConnectionconn=(HttpURLConnection)newURL(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));
OutputStreamoutStream=conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode()==200){
System.out.println("发送成功");
}else{
System.out.println("发送失败");
}
}
二、接受xml数据:
publicActionForwardgetXML(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
InputStreaminStream=request.getInputStream();
byte[]data=StreamTool.read(inStream);
Stringxml=newString(data,"UTF-8");
System.out.println(xml);
returnmapping.findForward("result");
}
希望本文所述对大家的Android程序设计有所帮助。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短