public class HttpDownFilesRequest extends AsyncTask{
private UserCallBack back;// 傳入接口
private String code;// 請(qǐng)求返回碼
private String Url;// 請(qǐng)求URL地址;
private Context context;// 上下文
private boolean heard;// 是否加session頭
private ProgressDialog dialog;// 對(duì)話框
private String seveFilepath;
int count = 0;
static boolean cancelUpgrade = false;
/**
* 請(qǐng)求構(gòu)造方法
*
* @param context
*? ? ? ? ? ? 上下文內(nèi)容
* @param back
*? ? ? ? ? ? 接口用于得到返回值
* @param code
*? ? ? ? ? ? 返回標(biāo)記code
* @param Url
*? ? ? ? ? ? 請(qǐng)求鏈接
* @param heard
*? ? ? ? ? ? 標(biāo)題頭
* @param seveFilepath
*? ? ? ? ? ? 保存文件路徑
*/
public HttpDownFilesRequest(Context context, UserCallBack back,
String seveFilepath, String code, String Url, boolean heard,
boolean IsCompress) {
this.back = back;
this.seveFilepath = seveFilepath;
this.code = code;
this.Url = Url;
this.context = context;
this.heard = heard;
}
@Override
protected String doInBackground(String... arg0) {
String result = "";
if (!NetUtils.checkNetworkInfo(context)) {
result = "網(wǎng)絡(luò)異常";
return "網(wǎng)絡(luò)未連接";
}
back.undayway(true);
/*
* 新增 7-26 @sunql
*/
HttpParams httpParameters = new BasicHttpParams();
httpParameters.setParameter(HTTP.UTF_8, HTTP.UTF_8);
// 設(shè)置 連接請(qǐng)求超時(shí)時(shí)間
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
// 設(shè)置 socket 讀取超時(shí)時(shí)間
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);
// 請(qǐng)求超時(shí)
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, Config.httpTimeOut);
HttpGet get = new HttpGet(Url);
HttpResponse response;
InputStream is = null;
FileOutputStream fileOutputStream = null;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
final long length = entity.getContentLength();
is = entity.getContent();
fileOutputStream = null;
if (is != null) {
File file = new File(seveFilepath);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
// 反饋下載進(jìn)度
publishProgress((int) (count * 100 / length));
if (cancelUpgrade)
break;
}
fileOutputStream.flush();
// 下載完成
if (count == length) {
result = "下載完成";
}
}
result = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
result = "下載失敗";
} catch (IOException e) {
result = "IO異常";
} catch (Exception e) {
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.e("DownloadThread", e.toString());
result = "IO異常";
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
Log.e("DownloadThread", e.toString());
result = "IO異常";
}
}
}
return result;
}
@Override
protected void onPostExecute(String result) {// 在doInBackground執(zhí)行完成后系統(tǒng)會(huì)自動(dòng)調(diào)用,result是返回值
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
String msg = "";
if (result.equals("下載完成")) {
context.startActivity(getApkFileIntent(seveFilepath));
back.onSuccess(result, code);// 請(qǐng)求成功接口
} else if (result.equals("網(wǎng)絡(luò)異常")) {
Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
back.onFail(result, code);
} else if (result.trim().equals("IO異常")) {
Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
back.onFail(result, code);// 請(qǐng)求失敗接口
} else if (result.equals("下載失敗")) {
Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
back.onFail(result, code);// 請(qǐng)求失敗接口
}
if (!msg.equals("")) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPreExecute() {// 執(zhí)行后臺(tái)耗時(shí)操作前執(zhí)行
if (!code.equals("下載插件...")) {
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("正在下載...");
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
for (int i = 0; i < progress.length; i++) {
System.out.println("progress" + i + "=" + progress[i]);
}
if (progress[0] > 0) {
dialog.setMessage("下載中...");
}
dialog.setProgress((int) (progress[0]));
}
// Android獲取一個(gè)用于打開(kāi)APK文件的intent
public static Intent getApkFileIntent(String param) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
return intent;
}
}