问题描述
我想要实现android客户端通过http连接的方式获取web服务端的xml文件,要怎么实现?
解决方案
程序入口 public static void getXMLForNet() { String httpPath = "填写您要获取xml数据的http地址,如果含有汉子则必须进行编码";String xml = getContent(httpParh, "UTF-8");FileOutputStream out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "test.xml"));OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");writer.write(xml);writer.flush();writer.close();out.close(); }public static String getContent(String path, String encoding) throws Exception{URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setReadTimeout(6*1000);if (conn.getResponseCode() == 200) {InputStream in = conn.getInputStream();byte[] data = readStream(in);return new String (data, encoding);}return null;}