开发者

Program freezes while download a file in android

I am getting MalformedURLexception in the following code, and i have no idea, whats causing it.

public void down(String url)
    {     try {
        URL url1 = new URL(url);


        HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();   
        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,"somefile.ext");


        FileOutputStream fileOutput = new FileOutputStream(file);

        InputStream inputStream = urlConnection.getInputStream();

        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;

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

        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //report progress


        }

        fileOutput.close();


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();

    }
    }
}

It says unknown protocol. The connection is totally fine unti the reading part comes up, before that the code is even printing right size of the file. Also the file i am trying to download has a url like http://download12.aomethin.com/blaa-blaa If i try to add www, the request starts redirecting. Although i think this might be a noobish but I also want to know how to get the name of this file and save file with that name instead of the one i choose.

Edit: the progr开发者_Python百科am is working now i just need to know how do i get the right name of the file. And make this a background process.


downloading on the main thread is not a good practice; you should implement it on a new thread; because while downloading, the main thread is busy for downloading the file. If the waiting time be more than expected the system will generate "Not responding" error.

In order to do that you can use "TaskAsync" or "IntentService" or even making a new thread. though, I suggest [TaskAsync] as it is easy to implement.

here is a good practice to set the file name yourself:

public void downloadClicked() {
    InputStream in = null;
    FileOutputStream f = null;      
    try {
        String path ="ftp://administrator:password@131.164.140.118:21/MyShedule.zip";
        String targetFileName = "MyShedule.zip";

        URL u = new URL(path);
        URLConnection c =  u.openConnection();         

        c.setDoOutput(true);
        c.connect();
        String string = Environment.getExternalStorageDirectory().getPath().toString();

        in = c.getInputStream();

        f = new FileOutputStream(new File(string +"/kidsLibrary/"+ targetFileName));

        IOUtils.copy(in, f);

        } catch (MalformedURLException e) {
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();            
        } catch (ProtocolException e) {         
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
        } catch (FileNotFoundException e) {        
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
        } catch (IOException e) {           
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
    } finally {
          IOUtils.closeQuietly(in);
          IOUtils.closeQuietly(f);
        }   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜