通过URL下载XML

时间:2015-07-29 15:53:01

标签: android android-activity

我们有一个Android应用程序,我们通过在设备上打开浏览器部署到客户URL,然后浏览器下载该文件。我们还尝试下载单独的.xml设置文件。如果我在浏览器中尝试这个,xml将只显示。在Android应用程序中,我可以从缓存中复制xml吗?

还有其他方法可以下载xml吗?

我可以使用不同的文件类型,浏览器会下载吗?

由于

2 个答案:

答案 0 :(得分:0)

以下是从网址下载xml文件的方法

try {
        //set the download URL, a url that points to a file 
        URL url = new URL("http://your/path/to/file.xml");

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.connect();

        //set the path where we want to save the file
        File SDCardRoot = Environment.getExternalStorageDirectory();
        //create a new file, specifying the path, and the filename
        //which we want to save the file as.
        File file = new File(SDCardRoot,"data.xml");

        //this will be used to write the downloaded data into the file we created
        FileOutputStream fileOutput = new FileOutputStream(file);

        //this will be used in reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        //this is the total size of the file
        int totalSize = urlConnection.getContentLength();
        progressDialog.setMax(totalSize);

        //variable to store total downloaded bytes
        int downloadedSize = 0;

        //create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0; //used to store a temporary size of the buffer

        //now, read through the input buffer and write the contents to the file
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;

        }
        //close the output stream when done
        fileOutput.close();
        //catch some possible errors...
} catch (MalformedURLException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

您应该在后台执行此操作,即使用AsyncTasc。

答案 1 :(得分:0)

如果需要加载一个小的xml文件,Document类可以直接使用。您可以使用http get请求来获取xml文件。这是一个简单的函数,你可以调用,传入一个url,并获得一个你可以挖掘的xml文档对象:

public static Document getXML(final String url) throws ExecutionException, InterruptedException {
        Document out = null;

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Document> result = executor.submit(new Callable<Document>() {
            public Document call() throws Exception {

                HttpGet uri = new HttpGet(url);

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse resp = null;
                try {
                    resp = client.execute(uri);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                StatusLine status = resp.getStatusLine();
                if (status.getStatusCode() != 200) {
                    Log.d("http error", "HTTP error, invalid server status code: " + resp.getStatusLine());
                }

                // try {
                HttpEntity entity = resp.getEntity();
                InputStream in = entity.getContent();

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = null;
                try {
                    builder = factory.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                }

                Document dom = null;
                try {
                    dom = builder.parse(in);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                }
                return dom;
            }

        });


        try {
            out = result.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return out;

    }