【发布时间】:2014-07-17 10:04:50
【问题描述】:
我正在为 Monitoring Cars App 做一个刷新按钮,所以当我点击它时,它会冻结 UI 几秒钟给定的线程,当我减少时间时,它不会正确获取数据(零或空数据)
点击监听器:
refreshcount.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
@Override
public synchronized void run() {
if (conDetector.isConnectingToInternet()) {
unitsRefreshTask = new AsyncRefreshUnit(
MonitoringController.usernamei,
MonitoringController.passwordi);
unitsRefreshTask.execute();
try {
Thread.sleep(5000);
//wait(11000);
} catch (InterruptedException e) {
e.printStackTrace();
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+" refreshcount.onclick() when calling AsyncRefreshUnit"+"\n"+e.getMessage().toString());
}
} else if (!conDetector.isConnectingToInternet()) {
showNoNet();
}
}
});
source = new CarsDataSource(getApplicationContext());
source.open();
MonitoringController.cars.clear();
MonitoringController.cars = new ArrayList<Car>();
ArrayList<Car> carList = new ArrayList<Car>();
System.out.println("task.getResult().size(); >>"
+ unitsRefreshTask.getResult().size());
carList = (ArrayList<Car>) unitsRefreshTask.getResult();
System.out.println("carList >>" + carList.size());
for (int u = 0; u < carList.size(); u++) {
System.out.println("carList.size() : >" + carList.size());
MonitoringController.cars.add(carList.get(u));
System.out.println("size"
+ MonitoringController.cars.size());
}
for (int i = 0; i < carList.size(); i++) {
SerialExist = source.isExistRoleAndSerial(getApplicationContext(),carList.get(i).getRoute_Nmae(),carList.get(i).getSerialNumber());
if(SerialExist){
source.updateInfoByroleName(getApplicationContext(),carList.get(i));
}else if(!SerialExist){
source.createCar(carList.get(i));
}
// System.out.println("update is >>" + update);
}
MonitoringController.countOfUnits = source.getCountsOfUnits(
getApplicationContext(), MonitoringController.routname);
Log.e("C_InformationScreen.class", source.getCountsOfUnits(
getApplicationContext(), MonitoringController.routname)+"");
((TextView) numOfUnits)
.setText(source.getCountsOfUnits(
getApplicationContext(), MonitoringController.routname) + " Units");
((TextView) numOfUnits)
.setText(carList.size() + " Units");
source.close();
}
});
异步任务:
public class AsyncRefreshUnit extends
AsyncTask<String, Void, ArrayList<Car>> {
String user, pass, resultstring;
ArrayList<Car> result = new ArrayList<Car>();
int count;
public List<Car> getResult() {
return result;
}
public void setResult(ArrayList<Car> result) {
this.result = result;
}
public AsyncRefreshUnit(String user, String pass) {
super();
this.user = user;
this.pass = pass;
}
@Override
protected void onPostExecute(ArrayList<Car> result) {
super.onPostExecute(result);
if(dialog.isShowing()){
dialog.dismiss();
}
initList();
}
@SuppressLint("NewApi")
private void finish(ArrayList<Car> result) {
if (isCancelled()) {
onCancelled(result);
} else {
onPostExecute(result);
}
Status mStatus = Status.FINISHED;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
dialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(C_InformationScreen.this,"","Refreshing Units Data ..");
super.onPreExecute();
}
@Override
protected ArrayList<Car> doInBackground(String... params) {
try {
httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(
InternetActions.getHttpParameters(110000), 70000);
HttpGet httpGet = new HttpGet(
"http://trackmonitor.net/software/AndroidControl.aspx?UserName="
+ user + "&PassWord=" + pass + "&Action=Units");
try {
HttpResponse response = httpClient.execute(httpGet);
int responsecode = response.getStatusLine().getStatusCode();
if (responsecode == 200) {
InputStream in = response.getEntity().getContent();
resultstring = MonitoringController.convertinputStreamToString(in);
try {
JSONArray plates = new JSONArray(resultstring);
// MyAssmts.clear();
for (int i = 0; i < plates.length(); ++i) {
JSONObject jo = (JSONObject) plates.get(i);
result.add(convertToCar(jo));
Log.v("check on adding car_id to list", result.get(i)
.getCarId() + "");
Log.v("check on adding car_name to list", result.get(i)
.getName());
Log.v("check on adding car_serial to list",
result.get(i).getSerialNumber());
Log.v("check on adding car_device to list",
result.get(i).getDeviceId());
Log.v("check on adding car_phone to list", result
.get(i).getPhone());
Log.v("check on adding car_role to list", result.get(i)
.getRoute_Nmae());
}
} catch (JSONException c) {
Log.v("Exception >>>", c.getMessage().toString());
c.printStackTrace();
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.JSONException "+"\n"+c.getMessage().toString());
}
long total = 0;
byte data[] = new byte[1024];
while ((count = in.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
setProgress((int)((total*100)/1000));
}
}else if(responsecode != 200){
Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.ClientProtocolException "+"\n"+e.getMessage().toString());
Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();
if (e.getMessage().indexOf("Connection reset by peer") > 0) {
Toast.makeText(getApplicationContext(),
"please Connect again ..", Toast.LENGTH_LONG)
.show();
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.IOException "+"\n"+e.getMessage().toString());
}
}
return result;
} catch (Exception t) {
t.printStackTrace();
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.Exception "+"\n"+t.getMessage().toString());
} finally {
if (in != null) {
try {
in.close();
if (httpClient != null)
httpClient.getConnectionManager().shutdown();
} catch (IOException x) {
Log.e("Exception > ", x.getMessage().toString());
MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground when closing connection "+"\n"+x.getMessage().toString());
}
}
}
return null;
}
【问题讨论】:
标签: java android multithreading android-asynctask