android客户端如何获取web服务端的xml文件
我想要实现android客户端通过http连接的方式获取web服务端的xml文件,要怎么实现?
程序入口
[code="java"]
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;
}
[/code]