6.2、Android中向Internet发送xml数据
向Internet发送xml数据
利用HttpURLConnection对象,我们可以向网络发送xml数据.
StringBuilderxml=newStringBuilder();
xml.append("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
xml.append("<M1V=10000>");
xml.append("<UI=1D=\"N73\">中国</U>");
xml.append("</M1>");
byte[]xmlbyte=xml.toString().getBytes("UTF-8");
URLurl=newURL("http://localhost:8080/itcast/contanctmanage.do?method=readxml");
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection","Keep-Alive");//维持长连接
conn.setRequestProperty("Charset","UTF-8");
conn.setRequestProperty("Content-Length",String.valueOf(xmlbyte.length));
conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8");
DataOutputStreamoutStream=newDataOutputStream(conn.getOutputStream());
outStream.write(xmlbyte);//发送xml数据
outStream.flush();
if(conn.getResponseCode()!=200)thrownewRuntimeException("请求url失败");
InputStreamis=conn.getInputStream();//获取返回数据
Stringresult=readAsString(is,"UTF-8");
outStream.close();
更多相关文章
- Android(安卓)添加程序到桌面favorites列表
- Android(安卓)4.4以上使用HttpURLConnection底层使用OkHttp实现
- Android(安卓)常见的几种加密方式RSA、CBC的集成
- Android(安卓)HandlerThread 消息循环机制之源码解析
- OkHttpClientManager使用
- android 百度地图定位功能实现
- Android中使用OrmLite来对SQLite数据库进行CRUD(增删改查)解析
- 浅析Android(安卓)手机卫士设备管理权限锁屏
- [置顶] Android(安卓)IPC 通讯机制源码分析【中】