- Java code
/** *下载 xml 文件**/StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); //访问互联网代码结束 -------- downloadpath = "http://www.kskjkf.com/version/update/"+1+".xml"; //建立下载文件的保存路径 String dirName= Environment.getExternalStorageDirectory()+"/update/"; System.out.println("下载文件的保存路径"+dirName); File file = new File(dirName); if(!file.exists()){ file.mkdir(); } //准备拼接新的文件名 newFileName = downloadpath.substring(downloadpath.lastIndexOf("/")+1, downloadpath.length()); //System.out.println("截取路径得到的名字是:"+newFileName); //newFileName = "update.xml"; newFileName = dirName+newFileName; System.out.println("新文件名字是:"+newFileName); File f = new File(newFileName); if(f.exists()){ System.out.println("如果存在就删除"); f.delete(); } try { URL url = new URL(downloadpath); URLConnection conn = url.openConnection(); System.out.println("连接文件路径"+url); int contentLength = conn.getContentLength(); System.out.println("文件长度===="+contentLength); InputStream is = conn.getInputStream(); byte[] bs = new byte[1024]; int len; OutputStream os = new FileOutputStream(newFileName); System.out.println("OS的长度"+os.toString()); while((len=is.read())!=-1){ os.write(bs,0, len); } System.out.println("下载完毕。。。。。"); Toast.makeText(UpdateActivity.this, "下载成功", Toast.LENGTH_LONG).show(); os.close(); is.close(); new Thread(new ResolveUpdateXml()).start(); } catch (Exception e) { e.printStackTrace(); } //new Thread(new ResolveUpdateXml()).start(); }
解析的时候报错:
- Java code
07-02 09:25:16.843: W/System.err(5366): java.io.IOException: Couldn't open ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????07-02 09:25:17.054: W/System.err(5366): Caused by: java.net.MalformedURLException: Protocol not found:
------解决方案--------------------
协议未找到?
URL url = new URL(downloadpath);
URLConnection conn = url.openConnection();
这两句应该执行有问题!
------解决方案--------------------
url构建错了 你可以试试你的你URL能直接访问么
我试过反正是不能访问