我正在使用Asynk Task从4G网络中的db槽中导出数据,如果网络连接不良,我会发生ANR崩溃
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
FetchUrl fetchUrl=new FetchUrl();
fetchUrl.execute();
return START_STICKY;
}
private class FetchUrl extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
ExportData dataExchange = new ExportData(getApplicationContext());
Date currentTime = Calendar.getInstance().getTime();
String format1 = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.ENGLISH).format(currentTime);
dataExchange.export_data(String.valueOf(format1).replace(":", "_"));
} catch (Exception e) {
return false;
}
return true;
}
}
在函数export_data中,数据已导出到ftp和mySQL服务器 在这里,我为ftp设置了timout
FTPClient ftpClient = new FTPClient();
try {
ftpClient.setDefaultTimeout(2000);
ftpClient.setConnectTimeout(2000);
ftpClient.setDataTimeout(2000);
ftpClient.setControlKeepAliveTimeout(2000);
在这里,我为mySql服务器设置超时
inal HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,3000);
HttpClient httpClient=new DefaultHttpClient(httpParams);
HttpPost httpPost=new HttpPost(strURL);