读的Andr​​oid遥控xml文件 [英] read the remote xml file in android

查看:135
本文介绍了读的Andr​​oid遥控xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在远程place.I一个XML文件要使用XML文件在我的Andr​​oid project.can任何人this.I正在采用Android 2.2给我一个例子code。

PS:我可以访问本地XML文件是在/ res文件夹。 我不知道XPath的事情。

解决方案

尝试{             //设置下载网址,网址指向的文件在互联网上             //这是要下载的文件             网址URL =新的URL(HTTP://IP/Downloads/data.xml);             //创建新的连接             HttpURLConnection的的URLConnection =(HttpURLConnection类)url.openConnection();             //并连接!             urlConnection.connect();             //设置的路径,我们要保存文件             //在这种情况下,将其保存在的根目录             //SD卡。             文件SDCardRoot = Environment.getExternalStorageDirectory();             //创建一个新的文件,指定路径和文件名             //这是我们要保存的文件。             档案文件=新的文件(SDCardRoot,data.xml中);             //这将被用于所下载的数据写入到我们创建的文件             FileOutputStream中fileOutput =新的FileOutputStream(文件);             //这将在从互联网读取数据被用于             InputStream中的InputStream = urlConnection.getInputStream();             //这是文件的总大小             INT totalSize = urlConnection.getContentLength();             progressDialog.setMax(totalSize);             //变量来存储下载的总字节             INT downloadedSize = 0;             //创建一个缓冲区...             byte []的缓冲区=新的字节[1024];             INT BufferLength中= 0; //用于存储缓冲器的临时尺寸             //现在,读取通过输入缓冲器和将内容写入到该文件             而(量(bufferLength = inputStream.read(缓冲液))大于0){                     //在缓冲器的数据添加到该文件中的文件输出流(该文件的SD卡上                     fileOutput.write(缓冲液,0,BufferLength中);                     //加起来的规模,所以我们知道有多少下载                     downloadedSize + = BufferLength中;             }             完成后//关闭输出流             fileOutput.close();             //捕捉一些可能出现的错误?     }赶上(MalformedURLException异常E){             e.printStackTrace();     }赶上(IOException异常E){             e.printStackTrace();     }

试试这个code下载您的XML文件,并检查本教程读取XML的 http://www.java-samples.com/showtutorial.php?tutorialid=152

I have a xml file in remote place.I want to use that xml file in my android project.can anyone give me a example code for this.I am using android 2.2.

P.S : I can access the local xml file which is in /res folder. I don't know anything about xPath.

解决方案

try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            URL url = new URL("http://IP/Downloads/data.xml");

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();



            //and connect!
            urlConnection.connect();

            //set the path where we want to save the file
            //in this case, going to save it on the root directory of the
            //sd card.
            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();
    }

Try this code for downloading your xml file and check this tutorial for reading xml http://www.java-samples.com/showtutorial.php?tutorialid=152

这篇关于读的Andr​​oid遥控xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆