Android 下载XML文件时出现异常

Android 下载XML文件时出现异常,android,xml,web-services,download,Android,Xml,Web Services,Download,我正在开发一个Android应用程序,我正在尝试从这个URL下载XML文件http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml并尝试将此文件保存到我的sd卡中 我想在点击按钮时下载文件。因此,我在单击按钮时使用以下代码 try { URL url = new URL("http://data.gov.in/sites/default/files/Date-Wise-Prices-

我正在开发一个Android应用程序,我正在尝试从这个URL下载XML文件
http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml
并尝试将此文件保存到我的sd卡中

我想在点击按钮时下载文件。因此,我在单击按钮时使用以下代码

try
    {
        URL url = new URL("http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml");

        //create the new connection

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

        //set up some things on the connection

        urlConnection.setRequestMethod("GET");

        urlConnection.setDoOutput(true);

        //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 = new File("/sdcard/"+"XML_folder/");

        //create a new file, specifying the path, and the filename

        //which we want to save the file as.

        File file = new File(SDCardRoot,"webservice.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();

        //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;

        int progress=(int)(downloadedSize*100/totalSize);

        //this is where you would do something to report the prgress, like this maybe

        //updateProgress(downloadedSize, totalSize);

        }

        //close the output stream when done

        fileOutput.close();
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
正如我使用的
try and catch
捕获异常并向我显示此消息
data.gov.in
(java.net.UnknownHostException:data.gov.in)


那么,现在该怎么办?

将权限放入清单文件

<uses-permission android:name="android.permission.INTERNET"/>

检查您的清单文件、是否授予权限,并使用AsyncTask或线程概念进行url连接(后台工作)


这个错误正在发生

  • 如果你忘了写这个
  • 如果您的互联网速度很慢或无法正常工作
  • 最后,如果您的服务器由于任何原因没有响应您的请求 试试这个

    URL url = new URL(Your URL);
    
                    //create the new connection
    
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
                    //set up some things on the connection
    
                    urlConnection.setRequestMethod("GET");
    
                    urlConnection.setDoOutput(true);
    
                    //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 = new File("/sdcard/");
    
                    //create a new file, specifying the path, and the filename
    
                    //which we want to save the file as.
    
                    File file = new File(SDCardRoot,"hello.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();
    
                    //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;
    
                    int progress=(int)(downloadedSize*100/totalSize);
    
                    //this is where you would do something to report the prgress, like this maybe
    
                    //updateProgress(downloadedSize, totalSize);
    
                    }
    
                    //close the output stream when done
    
                    fileOutput.close();
    

    我想下载XML文件及其类似的web服务…因此,如果有任何错误,请让我知道..“您请求的页面不可用”。@H.Moody:我已经提供了我要下载的链接,它也在我的浏览器中打开。那么,请确切地告诉我问题出在哪里?你在清单中添加了互联网权限了吗file@Droid:是的,现在我已经添加了权限,但是现在得到了这个异常
    java.io.FileNotFoundException:/sdcard/XML\u folder/webservice.XML(没有这样的文件或目录)
    帮助?你能解释一下你的概念吗。事实上,我是Android开发的新手。所以,这对我来说是件好事。@Shiv你在设备或模拟器中运行你的应用程序在哪里?你能发布你的清单文件吗?嘿,正如你所说的,我已经添加了使用互联网的权限
    ,但现在我得到了这个
    java.io.FileNotFoundException:/sdcard/XML\u文件夹/webservice.XML(没有这样的文件或目录)
    help??我在中运行我的应用程序device@Shiv文件根=新文件(Environment.getExternalStorageDirectory(),“/sdcard/XML_folder”);如果(!root.exists()){root.mkdirs();}文件gpxfile=新文件(根,“webservice.xml”);我怎么能忘记这一点呢..顺便说一句谢谢..但是现在我得到了
    java.io.FileNotFoundException:/sdcard/XML\u folder/webservice.XML(没有这样的文件或目录)
    XML没有下载到这个/sdcard/XML\u folder/webservice.XML位置检查中。!!我必须创建这个位置吗?我的问题解决了。现在,您可以帮助我
    如何在我的应用程序中使用此XML文件数据吗`
    
             ibut.setOnClickListener(new View.OnClickListener() 
            {
           public void onClick(View v) 
              {
                new Thread(new Runnable() 
                    {
                      public void run() 
                        {
                    try
                     {
                         //http connection code. 
                     }      
                              catch(Exception e) 
                             {
                            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
                             }
    
                    }).start();
    
    
            }}); 
    
    URL url = new URL(Your URL);
    
                    //create the new connection
    
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
                    //set up some things on the connection
    
                    urlConnection.setRequestMethod("GET");
    
                    urlConnection.setDoOutput(true);
    
                    //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 = new File("/sdcard/");
    
                    //create a new file, specifying the path, and the filename
    
                    //which we want to save the file as.
    
                    File file = new File(SDCardRoot,"hello.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();
    
                    //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;
    
                    int progress=(int)(downloadedSize*100/totalSize);
    
                    //this is where you would do something to report the prgress, like this maybe
    
                    //updateProgress(downloadedSize, totalSize);
    
                    }
    
                    //close the output stream when done
    
                    fileOutput.close();