简体   繁体   English

AsyncTask中的线程可见性

[英]Thread visibility in AsyncTask

The following code creates a background task and executes it. 以下代码创建一个后台任务并执行它。

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 and destinationCode are instance variables. originCodedestinationCode是实例变量。

The following is what the background task does. 以下是后台任务的工作。

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);
    }

}

The following is part of what the http call does. 以下是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);
            ...
}

The following is the URL that results. 以下是产生的URL。

http://192.168.0.150/innobussvr/BusSchedulesSearchByOrgDestStartTimeEndTime/null/null

I understand that this is a thread visibility problem. 我了解这是线程可见性问题。 How can I solve it? 我该如何解决?

简单解决方案只需为RequestScheduleTask创建一个使用2个String的Constractor并将其传递,然后在Task类2个本地字符串var中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM