MalformedURLException的android系统中下载一个压缩文件时, [英] MalformedURLException in android when downloading a zip file

查看:142
本文介绍了MalformedURLException的android系统中下载一个压缩文件时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我M下载android系统中的zip,得到了以下错误。

InputStream的连接被抛出空的例外是

  java.net.MalformedURLException:协议未找到:www.songspk320.in/128/indian/Don-2-2011-128Kbps(Songs.PK).zip
    在与的java.net.URL下。INIT>(URL.java:273)
    在与的java.net.URL下。INIT>(URL.java:157)
    在com.linkezzi.web.DownloadIconSetZIP.openInputStreamConnection(DownloadIconSetZIP.java:73)
    在com.linkezzi.web.DownloadIconSetZIP.doInBackground(DownloadIconSetZIP.java:45)
    在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
    在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)
    在java.util.concurrent.FutureTask.run(FutureTask.java:138)
    在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
    在java.lang.Thread.run(Thread.java:1019)

code如下。

 公共类DownloadIconSetZIP扩展的AsyncTask {        私人文件根= NULL;
        私人字符串URL = NULL;
        私人文件pathToLinkEziiIconSetRootFolder = NULL;
        私人文件toBeExtracted = NULL;
        私人的FileOutputStream FOUT = NULL;
        私人语境mContext = NULL;
        私人的InputStream输入= NULL;
        公共DownloadIconSetZIP(上下文mContext,弦乐uriZIP){            this.url = uriZIP;
            this.mContext = mContext;
        }
        保护对象doInBackground(Object []对象PARAMS){            如果(this.url!= NULL){                根= Environment.getExternalStorageDirectory();                尝试{                    Log.e(下载图标集背景,URL =+网址);                    openInputStreamConnection();                    Log.e(图标集是从URI下载=,url.toUpperCase());                    pathSetInLocalFileSystem();                    downloadFile(输入);
                }赶上(MalformedURLException的MalformedURLException的){                    malformedURLException.printStackTrace();                }赶上(例外五){                    e.printStackTrace();                }            }            返回null;
        }
        私人无效openInputStreamConnection()抛出IOException异常,
                MalformedURLException的{
            尝试{
                HttpURLConnection的康恩=(HttpURLConnection类)新的URL(网址).openConnection();
// conn.setDoInput(真);
                conn.setConnectTimeout(30000); //超时10秒
                conn.connect();
                输入= conn.getInputStream();
            }赶上(MalformedURLException的E){
                e.printStackTrace();
            }        }
        私人无效downloadFile(输入的InputStream)抛出IOException
            INT BYTECOUNT = 0;
            字节[]缓冲区=新的字节[1024];
            INT读取动作= -1;
            而((读取动作= input.read(缓冲液))!= - 1){                fOut.write(缓冲液,0,读取动作);
                BYTECOUNT + =读取动作;            }
            fOut.flush();
            fOut.close();
        }        私人无效pathSetInLocalFileSystem()抛出FileNotFoundException异常{
            pathToLinkEziiIconSetRootFolder = searchFileInThisDirectory(根);            如果(pathToLinkEziiIconSetRootFolder == NULL){
                pathToLinkEziiIconSetRootFolder =新的文件(root.getAbsolutePath(),LinkEziiIconsets);                如果(pathToLinkEziiIconSetRootFolder.exists()及!&放大器; pathToLinkEziiIconSetRootFolder.mkdir()){
                    Log.e(LinkEzii图标集集装箱文件夹中创建,LinkEzii图标集集装箱文件夹中创建);
                }其他{
                    Log.e(LinkEzii图标集集装箱未创建,LinkEzii图标集集装箱未创建);
                }
            }            文件downloadedIconSetFile =新的文件(pathToLinkEziiIconSetRootFolder.getAbsolutePath(),IconSetName来这里......);            如果(!downloadedIconSetFile.exists()){
                downloadedIconSetFile.mkdir();
                Log.e(容器中创建的图标集,容器中创建的图标集);
            }
            如果(downloadedIconSetFile!= NULL){
                toBeExtracted =新的文件(downloadedIconSetFile图标集拉链Format.zip);
                FOUT =新的FileOutputStream(toBeExtracted);
            }        }
        私人文件searchFileInThisDirectory(档案文件)抛出NullPointerException异常{            如果(文件== NULL){
                抛出新的NullPointerException();
            }            文件[] listFiles = file.listFiles();            文件pathToLinkEziiIconSetRootFolder = NULL;            对于(文件searchIT:listFiles){
                如果(file.isDirectory()及&放大器; file.getName()等于(LinkEziiIconsets)。){
                    pathToLinkEziiIconSetRootFolder =文件;                    返回pathToLinkEziiIconSetRootFolder;
                }
            }            返回pathToLinkEziiIconSetRootFolder;        }        保护无效onCancelled(){
            super.onCancelled();
            Log.i(下载被取消,下载被取消);
        }        保护无效onPostExecute(对象结果){
                super.onPostExecute(结果);// DECOM pressing文件
            如果(toBeExtracted!= NULL){//只是为了移动DECOM pressing到回地面主题
                新主题(新的Runnable接口(){                    @覆盖
                    公共无效的run(){                        DECOM pressDownloadedDiagram DECOM pressdDownloadedDiagram =新DECOM pressDownloadedDiagram(toBeExtracted.getPath(),toBeExtracted.getPath(),toBeExtracted);
                        DECOM pressdDownloadedDiagram.unzip();
                        toBeExtracted.delete();
                    }
                })。开始();             Toast.makeText(mContext图标设置成功下载,Toast.LENGTH_LONG).show();            }其他{
                Toast.makeText(mContext,错误而下载图!,Toast.LENGTH_LONG).show();
                Log.e(一些错误Occure边下载边图,一些错误Occure在下载图);
            }            Log.i(下载成功,下载成功);
        }        在preExecute保护无效(){
            super.on preExecute();
            Log.i(下载已开始,下载已开始);
        }        保护无效onProgressUpdate(对象[]值){
            super.onProgressUpdate(值);            Log.i(Downlaoding正在进行中,下载过程);
        }
    }


解决方案

MalformedURLException的普遍抛出时,有没有附上你必须检查的任何协议是否是系统协议,通过检查:// 为present在你的URL字符串。如果您的网址是具有 WWW 你可以用整个检查 HTTP ://

查找 HTTP的发生:通过调用// 包含()方法上的网址字符串。如果你能找到检查他们是否开始字符串调用字符 url.startsWith。(HTTP://),以确保该异常不能被抛出

如果上述测试通过刚刚加入这一行。

URL =HTTP://+ URL;

I m download a zip in android and got following errors.

InputStream Connection is null exception thrown is

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)

Code is Given Below.

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");
        }


    }

解决方案

MalformedURLException generally thrown when there is no protocol attached with the system you have to check whether any protocol is that by checking whether :// is present in your URL string.. If your url is having www you can check it with whole http://.

Find occurrence of http:// by calling contains() method on your url string. If you can find check whether they are starting characters of string call url.startsWith("http://") to make sure this exception can not be thrown.

If none of above tests pass just add this line.

url = "http://"+url;

这篇关于MalformedURLException的android系统中下载一个压缩文件时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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