以下代码创建后台任务并执行它。
String dateString = null;
if (dateSelected)
dateString = Utils.parseDateToMsTimestamp(selectedDate);
final String ori = originCode;
final String dest = destinationCode;
RequestScheduleTask requestScheduleTask = new RequestScheduleTask();
requestScheduleTask
.execute(ori, dest, dateString);
originCode
和destinationCode
是实例变量。
以下是后台任务的作用。
private class RequestScheduleTask extends
AsyncTask<String, Void, List<CUSchedule>> {
@Override
protected List<CUSchedule> doInBackground(String... args) {
List<CUSchedule> cuSchedules = null;
try {
cuSchedules = CURestCommunicator
.requestSUScheduleByOriginAndDestination(args[0],
args[1], args[2]);
} catch (NetworkException e) {
}
return cuSchedules;
}
@Override
protected void onPostExecute(List<CUSchedule> result) {
if (result == null) {
raiseError("Server Error");
}
InnoBusApplication innoBusApplication = (InnoBusApplication) getApplication();
innoBusApplication.setCuSchedules(result);
super.onPostExecute(result);
}
}
以下是http调用的一部分。
public static List<CUSchedule> requestSUScheduleByOriginAndDestination (
String origin, String destination, String date) throws NetworkException {
Log.d("upload", "up");
origin = Utils.shortNameForCity(origin);
destination = Utils.shortNameForCity(destination);
HttpClient client = null;
String url = "http://" + SVR + "/innobussvr/BusSchedulesSearchByOrgDestStartTimeEndTime/"
+ origin + "/" + destination;
Log.d("url", url);
...
}
以下是产生的网址。
http://192.168.0.150/innobussvr/BusSchedulesSearchByOrgDestStartTimeEndTime/null/null
我知道这是一个线程可见性问题。我该如何解决?
答案 0 :(得分:0)
Simple Solution只需为Request Scheduled Task创建一个Constructor,它接受2个String并传递它们,然后在Task class 2本地字符串var中创建。