protected void downXMLFile(final String url,Handler mHandler) {
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 10000);
try {
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is == null) {
throw new RuntimeException("isStream is null");
}
String filename = HiStoreFileMutileDownLoad
.getFilenName(url);
File file = new File(XMLPath, filename);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int percent = 0;
int prepercent = -1;
writeSize = 0;
do {
ch = is.read(buf);
if (ch <= 0)
break;
fileOutputStream.write(buf, 0, ch);
writeSize += ch;
percent = (int) ((writeSize * 100) / length);
// Log.i(TAG, "ch =" + ch + " writeSize="
// + writeSize + " percent=" + percent
// + " prepercent=" + prepercent);
} while (true);
is.close();
fileOutputStream.close();
if (mhandler != null&&url.equals(xmlfileUrl[0])) {//下载完成后通知更新界面,解决有时第一开启动histore时由于下载未完成而获取不到数据的问题
mhandler.sendEmptyMessage(0x004);
}
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
Log.e(TAG, "update packaget not found!!! ");
IshostOK++;
if (IshostOK >= 3) {
IshostOK = 0;
if (mhandler != null) {
mhandler.sendEmptyMessage(0x003);
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// 下载xml,如果失败则可以推出服务器不稳定,从而用handler发出信号给showdialog
IshostOK++;
if (IshostOK >= 3) {
IshostOK = 0;
if (mhandler != null) {
mhandler.sendEmptyMessage(0x003);
}
}
Log.v(TAG, "update packaget not found!!! IshostOK = "
+ IshostOK);
e.printStackTrace();
}
}
}.start();
}