Android HttpURLConnection 下载xml文件时候 出现ioexception
来源:互联网 发布:javascript格式化输出 编辑:程序博客网 时间:2024/06/05 17:12
1.参考http://blog.csdn.net/maxracer/article/details/7096000
下面这段代码发在java 项目下是没问题的,放在android 下就出现了问题,而且下载其他格式文件的时候也没有出现问题,再下载xml后缀文件
conn.getContentLength() 出现-1 ,这里也跟机型有关,有些手机是会出现,有些没出现,没有出现的-1的在执行到InputStream inStream = http.getInputStream(); 这里的时候也会抛异常,根据参考的 加入conn.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码 完美解决
public static void tt() throws Exception {
String downloadUrl = "http://www.ubtrobot.com/tools/alpha1robot/android/version.xml";
URL url = new URL(downloadUrl);// 打开HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置 HttpURLConnection的断开时间
conn.setConnectTimeout(5000);
// 设置 HttpURLConnection的请求方式
conn.setRequestMethod("GET");
// 设置 HttpURLConnection的接收的文件类型
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml, "
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, "
+ "application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
// 设置 HttpURLConnection的接收语音
conn.setRequestProperty("Accept-Language", Locale.getDefault()
.toString());
// 指定请求uri的源资源地址
conn.setRequestProperty("Referer", downloadUrl);
// 设置 HttpURLConnection的字符编码
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
printResponseHeader(conn);
if (conn.getResponseCode() == 200) {
int fileSize = conn.getContentLength();// 根据响应获取文件大小
if (fileSize <= 0)
throw new RuntimeException("Unkown file size ");
String filename = "zdy.xml";// getFileName(conn);// 获取文件名称
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int offset = 0;
RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd");
while ((offset = inStream.read(buffer, 0, 1024)) != -1) {
threadfile.write(buffer, 0, offset);
}
int x = 0;
int y = 1;
}
}
///这里又是线程一个方法的
if (downLength < block) {// 未下载完成
try {
HttpURLConnection http = (HttpURLConnection) downUrl
.openConnection();
http.setConnectTimeout(5 * 1000);
http.setRequestMethod("GET");
http.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
http.setRequestProperty("Accept-Language", "zh-CN");
http.setRequestProperty("Referer", downUrl.toString());
http.setRequestProperty("Charset", "UTF-8");
int startPos = block * (threadId - 1) + downLength;// 开始位置
int endPos = block * threadId - 1;// 结束位置
http.setRequestProperty("Range", "bytes=" + startPos + "-"
+ endPos);// 设置获取实体数据的范围
http.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
http.setRequestProperty("Connection", "Keep-Alive");
http.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码
InputStream inStream = http.getInputStream();
byte[] buffer = new byte[1024];
int offset = 0;
print("Thread " + this.threadId
+ " start download from position " + startPos);
RandomAccessFile threadfile = new RandomAccessFile(
this.saveFile, "rwd");
threadfile.seek(startPos);
while (!downloader.getExit()
&& (offset = inStream.read(buffer, 0, 1024)) != -1) {
threadfile.write(buffer, 0, offset);
downLength += offset;
downloader.update(this.threadId, downLength);
downloader.append(offset);
}
threadfile.close();
inStream.close();
print("Thread " + this.threadId + " download finish");
this.finish = true;
} catch (Exception e) {
this.downLength = -1;
print("Thread " + this.threadId + ":" + e);
}
}
- Android HttpURLConnection 下载xml文件时候 出现ioexception
- Android文件下载 HttpURLConnection
- android 文件下载详解(HttpURLConnection)
- Android学习之使用HttpURLConnection下载文件
- android利用HttpURLConnection+XmlPullParser解析Xml文件
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- HttpUrlConnection实现文件下载
- 使用HttpURLConnection下载文件
- HttpURLConnection实现文件下载
- javaIE下载文件中取消下载出现ClientAbortException: java.io.IOException异常
- android HttpURLConnection 实现下载
- Android 使用HttpURLConnection 下载
- Android HttpURLConnection 多线程下载
- android多线程下载-HttpURLConnection
- [Android]解决HttpURLConnection上传大文件时出现OOM问题
- Android - HttpURLConnection下载网络文件,加载网络图片
- Android下载文件出现问题
- VC利用console调试和记录日志
- JdbcTemplate学习笔记
- Running Oracle ADF application on High availability (HA) architecture by Vinay Kumar
- Linux 用dd生成指定大小的文件
- MSVC CRT的全局构造和析构
- Android HttpURLConnection 下载xml文件时候 出现ioexception
- 提取VS的Win32SDk用C/C++编译器 (续)——使用方法
- jquery 动态设置图片居中显示
- 我的MYSQL学习心得(1) 简单语法
- JavaScript实现图片上标记多点区域
- AVFoundation的使用
- 登陆页面的用户信息保存实现
- 2- Linux系统入门
- ios开发网络篇 - 使用ASI框架进行文件下载