Android 网络连接异常
Android 网络连接错误
最近写了一个多线程下载的Android程序,但是不能正常运行,不能正常连接网络,什么原因?(ps:网络环境正常,可以上百度),代码如下:
错误log输出如下,我打印了一下conn.getResponseCode() = 403,貌似是死在InputStream inputStream = conn.getInputStream();这句了,但是到底是什么原因啊,求大神帮忙看一下
------解决思路----------------------
看代码好像无措,conn.setRequestProperty("Range", "byte ="+startpositon+"-");这句话是不是设置有问题,,,,
------解决思路----------------------
------解决思路----------------------
你这个问题找的我真苦啊,试了半个消失才发现原来错误在这里
http.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);//设置获取实体数据的范围
这是正确写法 应该是bytes,你少了了个s
最近写了一个多线程下载的Android程序,但是不能正常运行,不能正常连接网络,什么原因?(ps:网络环境正常,可以上百度),代码如下:
/*
* 开始正常下载
*
*/
public void download() throws Exception{
downloadSave = new DownloadSave();
forder = downloadSave.DirFile("Myfile");
file = downloadSave.createFile("Myfile/QQ.exe");
path = "http://dlsw.baidu.com/sw-search-sp/soft/3a/12350/QQ5.1.10055.0.1393407862.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(6*1000);
conn.setRequestMethod("GET");
int filelength = conn.getContentLength();
RandomAccessFile randomfile = new RandomAccessFile(file, "rw");
randomfile.setLength(filelength);
randomfile.close();
conn.disconnect();
int thread = 3;
int threadlength = filelength%thread ==0 ? filelength/thread:filelength/thread+1;
for(int i=0; i<thread; i++){
int startposition = i * threadlength;
RandomAccessFile threadfile = new RandomAccessFile(file,"rw");
threadfile.seek(startposition);
new DownloadThread(i, path, startposition,
threadfile, threadlength).start();
}
}
public class DownloadThread extends Thread{
private int threadid;
private String path;
private int startpositon;
private RandomAccessFile file;
private int filelength;
public DownloadThread(int threadid, String path,int startpositon,
RandomAccessFile file, int threadlength){
this.threadid = threadid;
this.path = path;
this.startpositon = startpositon;
this.file = file;
this.filelength = threadlength;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(6*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "byte ="+startpositon+"-");
InputStream inputStream = conn.getInputStream();
byte[] data = new byte[1024];
int len = 0;
int length = 0;
while(length< filelength&&(len=inputStream.read(data))!=-1){
file.write(data,0,len);
length += len;
}
inputStream.close();
file.close();
System.out.println("线程"+(threadid+1)+" over");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("线程"+(threadid+1)+" 出错");
}
}
}
错误log输出如下,我打印了一下conn.getResponseCode() = 403,貌似是死在InputStream inputStream = conn.getInputStream();这句了,但是到底是什么原因啊,求大神帮忙看一下
------解决思路----------------------
看代码好像无措,conn.setRequestProperty("Range", "byte ="+startpositon+"-");这句话是不是设置有问题,,,,
------解决思路----------------------
URL url = new URL(这是我写的,可以参考下,因为你那里设置conn属性,很少会用,我也不知道,感觉可能出错,,,
"http://192.168.191.104:8080/myapp/servlet/MyServlet");
HttpURLConnection connection = ((HttpURLConnection) url
.openConnection());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();
OutputStream out = connection.getOutputStream();
int len;
byte[] buffer = new byte[1024];
// 读取文件
FileInputStream fileInputStream = new FileInputStream(
Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/123.jpg");
while((len = fileInputStream.read(buffer, 0, 1024)) != -1){
out.write(buffer);
}
out.flush();
out.close();
fileInputStream.close();
InputStream input = connection.getInputStream();
while ((len = input.read(buffer)) != -1) {
Log.i("tag", "data:"
+ new String(buffer, 0, len));
}
input.close();
connection.disconnect();
------解决思路----------------------
你这个问题找的我真苦啊,试了半个消失才发现原来错误在这里
http.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);//设置获取实体数据的范围
这是正确写法 应该是bytes,你少了了个s