/**
 *下载 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();
}解析的时候报错:
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: 

解决方案 »

  1.   

    协议未找到?  
    URL url = new URL(downloadpath);
    URLConnection conn = url.openConnection();
    这两句应该执行有问题!
      

  2.   

    应该不是的 这句话下面的 长度还以可以印出来呢  他说协议没找到开始我以为下载的文件是错的 就直接换了个文件在这个路径下面  他解析的时候还是报这个错。 
    int contentLength = conn.getContentLength();                System.out.println("文件长度===="+contentLength);
      

  3.   

    url构建错了 你可以试试你的你URL能直接访问么
    我试过反正是不能访问