HttpClient client = SSLSocketFactoryEx.getNewHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
//get请求加入参数
String content =null;
StringBuilder sb =new StringBuilder();
sb.append(remoteUrl);
Iterator it =map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
try {
sb.append("&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
content = sb.toString();
HttpGet get =new HttpGet(content);
try {
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if (200 == status) {
if (response.getEntity() ==null){
callback.onError("response error");
}
length = response.getEntity().getContentLength();
localInputStream = response.getEntity().getContent();
//在本地存储文件
File localFile =new File(localFilePath);
localFileOutputStream =new FileOutputStream(localFile);
int bufsize = CommonUtils.getDownloadBufSize(this.context);
byte[] arrayOfByte =new byte[bufsize];
while ((i = localInputStream.read(arrayOfByte)) != -1) {
rate += i;
if (length >0) {
int progress = (int) (rate *100L / length); //下载进度
if (callback !=null) {
callback.onProgress(progress); //可以在回调中进行进度条更新
}
}
localFileOutputStream.write(arrayOfByte, 0, i);
}
callback.onSuccess(null);
}else {
if (callback !=null) {
callback.onError(null);
}
}
//后边的资源释放,异常捕获都省略了 ,只展示部分代码