下载zip文件时android中的MalformedURLException

时间:2011-12-15 16:11:00

标签: android downloading malformedurlexception

我在Android下载了一个zip并遇到了以下错误。

InputStream Connection为null异常抛出

java.net.MalformedURLException: Protocol not found: www.songspk320.in/128/indian/Don-2-2011-128Kbps(Songs.PK).zip
    at java.net.URL.<init>(URL.java:273)
    at java.net.URL.<init>(URL.java:157)
    at com.linkezzi.web.DownloadIconSetZIP.openInputStreamConnection(DownloadIconSetZIP.java:73)
    at com.linkezzi.web.DownloadIconSetZIP.doInBackground(DownloadIconSetZIP.java:45)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)

下面给出了代码。

public class DownloadIconSetZIP extends AsyncTask {

        private File root  = null;
        private String url = null;
        private File pathToLinkEziiIconSetRootFolder = null;
        private File toBeExtracted = null;
        private FileOutputStream fOut  = null;
        private Context mContext = null;
        private InputStream input  = null;


        public DownloadIconSetZIP(Context mContext,String uriZIP){

            this.url = uriZIP;
            this.mContext = mContext;
        }
        protected Object doInBackground(Object[] params) {

            if(this.url != null){

                root = Environment.getExternalStorageDirectory();

                try {

                    Log.e("Downloading Iconset in BackGround","URl = "+ url);

                    openInputStreamConnection();

                    Log.e("Iconset is Downloading from the URI  = ",url.toUpperCase());

                    pathSetInLocalFileSystem();

                    downloadFile(input);


                }catch(MalformedURLException malformedURLException){

                    malformedURLException.printStackTrace();

                }catch (Exception e) {

                    e.printStackTrace();

                }

            }

            return null;
        }
        private void openInputStreamConnection() throws IOException,
                MalformedURLException {


            try {
                HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
//              conn.setDoInput(true);
                conn.setConnectTimeout(30000); // timeout 10 secs
                conn.connect();
                input = conn.getInputStream();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

        }
        private void downloadFile(InputStream input) throws IOException {
            int byteCount = 0;
            byte[] buffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = input.read(buffer)) != -1) {

                fOut.write(buffer, 0, bytesRead);
                byteCount += bytesRead;

            }
            fOut.flush();
            fOut.close();
        }

        private void pathSetInLocalFileSystem() throws FileNotFoundException {


            pathToLinkEziiIconSetRootFolder = searchFileInThisDirectory(root);

            if(pathToLinkEziiIconSetRootFolder == null){
                pathToLinkEziiIconSetRootFolder = new File(root.getAbsolutePath(),"LinkEziiIconsets");

                if(!pathToLinkEziiIconSetRootFolder.exists() && pathToLinkEziiIconSetRootFolder.mkdir()){
                    Log.e("LinkEzii Iconset Container Folder is Created","LinkEzii Iconset Container Folder is Created");
                }else{
                    Log.e("LinkEzii Iconset Container is Not Created","LinkEzii Iconset Container is Not Created");
                }
            }

            File downloadedIconSetFile = new File(pathToLinkEziiIconSetRootFolder.getAbsolutePath(),"IconSetName Comes Here...");

            if(!downloadedIconSetFile.exists()){
                downloadedIconSetFile.mkdir();
                Log.e("Container is Created for Iconset","Container is Created for iconset");
            }


            if(downloadedIconSetFile!=null){
                toBeExtracted = new File(downloadedIconSetFile, "IconSet Zip Format.zip");
                fOut = new FileOutputStream(toBeExtracted);
            }

        }
        private File searchFileInThisDirectory(File file) throws NullPointerException {

            if(file == null){
                throw new NullPointerException();
            }

            File[] listFiles = file.listFiles();

            File pathToLinkEziiIconSetRootFolder = null;

            for(File searchIT: listFiles){
                if(file.isDirectory() && file.getName().equals("LinkEziiIconsets")){
                    pathToLinkEziiIconSetRootFolder = file;

                    return pathToLinkEziiIconSetRootFolder;
                }
            }

            return pathToLinkEziiIconSetRootFolder;

        }

        protected void onCancelled() {
            super.onCancelled();
            Log.i("Downloading is Cancelled","Download is Cancelled");
        }

        protected void onPostExecute(Object result) {
                super.onPostExecute(result);

//          Decompressing the File
            if(toBeExtracted != null){

//              Just to Move the Decompressing  to the Back Ground Thread
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        DecompressDownloadedDiagram decompressdDownloadedDiagram = new DecompressDownloadedDiagram(toBeExtracted.getPath(), toBeExtracted.getPath(),toBeExtracted);
                        decompressdDownloadedDiagram.unzip();
                        toBeExtracted.delete();
                    }
                }).start();

             Toast.makeText(mContext, "Icons Set is Successfully Downloaded", Toast.LENGTH_LONG).show();

            }else{
                Toast.makeText(mContext, "Error! while downloading diagram", Toast.LENGTH_LONG).show();
                Log.e("Some Error Occure While Downloading Diagram","Some Error Occure While Downloading Diagram");
            }

            Log.i("Downloading is Successful","Downloading is Successful");
        }

        protected void onPreExecute() {
            super.onPreExecute();
            Log.i("Downloading is Starting","Downloading is Starting");
        }

        protected void onProgressUpdate(Object[] values) {
            super.onProgressUpdate(values);

            Log.i("Downlaoding is in Progress","Downloading is in Progress");
        }


    }

1 个答案:

答案 0 :(得分:3)

MalformedURLException通常在系统没有附加协议时抛出,您必须通过检查URL字符串中是否存在://来检查是否有任何协议。如果您的网址有www您可以使用整个http://进行检查。

通过在http://字符串上调用contains()方法查找url的匹配项。如果你能找到它们是否正在启动字符串调用url.startsWith("http://")的字符,以确保不能抛出此异常。

如果上述测试都没有通过,只需添加此行。

url = "http://"+url;