/**
*下载 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:
解决方案 »
- 安卓原生浏览器源代码 有错该如何解决?
- 如何给定屏幕作标,触发单击屏幕事件
- 实现安卓桌面壁纸设置的接口
- 如何通过URI获取数据库表指定列的数据类型?
- android系统input子系统keypad的问题~
- Android适用那些手机?兼容性如何?市场占有份额如何?
- [菜鸟求指导]怎样把dataIputStream直接赋给dataOutputStream呢
- source not found这个怎么解决啊
- 融云eclipse集成 demo项目出现的问题,求大神指导
- 请问这个怎么修改,谢谢。
- 为什么我在解析的网站的信息在添加数据库的时候只添加一条信息。。。。
- 谁有客户端和服务端对接的代码,麻烦给我发一下,谢谢
URL url = new URL(downloadpath);
URLConnection conn = url.openConnection();
这两句应该执行有问题!
int contentLength = conn.getContentLength(); System.out.println("文件长度===="+contentLength);
我试过反正是不能访问