我正在制作android App,我需要xml
从一个文件下载URL
并打开它,我该怎么做?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File dir1 = getDir("xmls",Context.MODE_PRIVATE);//Creating an internal dir;
System.out.println("dir1: " + dir1);
//Saving The File
try {
URL url = new URL("http://www. the url of my xml .xml");
// The server thinks this request is from an Opera browser!
String userAgent = "Opera/9.63 (Windows NT 5.1; U; en) Presto/2.1.1";
System.out.println("Downloading ...");
downloadFromUrl(url, dir1+"news.xml", userAgent);
System.out.println("OK");
} catch (Exception e) {
System.err.println(e);
System.out.println("Entri pure nel catch");
}
//This is the path of the xml file that i have saved
URL = dir1+"/news.xml" ;
ArrayList<HashMap<String, String>> songsList =
new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from UR
Document doc = parser.getDomElement(xml); // getting DOM element
//And then i do all the parsing
NodeList nl = doc.getElementsByTagName(KEY_CATEGORIA);
XMlParser
类是这样的:
public class XMLParser_Categorie {
// constructor
public XMLParser_Categorie() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
我需要更改此指令:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
它是来自本地目录的请求,而不是 http 请求。