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

0 0
原创粉丝点击