Android HttpURLConnection 下载xml文件时候 出现ioexception

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中的HttpURLConnection是一个用于发送和接收HTTP请求的类。它是Java标准库中的一部分,并且在Android开发中被广泛使用。 使用HttpURLConnection可以执行以下操作: 1. 创建连接:使用URL对象创建HttpURLConnection对象,并设置连接的URL。 2. 设置请求方法:使用setRequestMethod()方法设置请求的方法,如GET、POST等。 3. 设置请求头:使用setRequestProperty()方法设置请求头信息,如Content-Type、User-Agent等。 4. 设置请求体:对于POST请求,可以使用OutputStream将数据写入请求体。 5. 发送请求:使用connect()方法建立与服务器的连接,并发送请求。 6. 获取响应码:使用getResponseCode()方法获取服务器的响应码。 7. 获取响应数据:根据响应码,可以使用getInputStream()或getErrorStream()方法获取服务器返回的数据流。 8. 解析响应数据:根据服务器返回的数据格式,可以使用相应的解析方式进行解析,如JSON解析、XML解析等。 以下是一个简单的示例代码,演示了如何使用HttpURLConnection发送GET请求并获取响应数据: ```java try { URL url = new URL("http://www.example.com/api/data"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); connection.disconnect(); // 处理响应数据 String responseData = response.toString(); // ... } else { // 处理错误情况 } } catch (IOException e) { e.printStackTrace(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值