如何强制异步任务等待答案?

时间:2019-04-25 14:25:55

标签: android asynchronous synchronous openweathermap

我正在使用一个异步库来获取天气预报,但是即使我没有收到有关天气的信息,我的代码仍然继续。有什么办法等待答案吗?还是您知道有什么可以帮助我的吗?

3 个答案:

答案 0 :(得分:0)

代码继续进行就可以了。您应该使用回调来接收数据。有关更多详细信息,请附加您的功能。

答案 1 :(得分:0)

WeatherClass.java

public class WeatherClass {

    private static double temp = 0.0;


    public static double getTemp(double latitude, double longitude, Context context){
        String OPEN_WEATHER_MAP_API = "xxxxxxxxx";
        new WeatherManager(OPEN_WEATHER_MAP_API).getFiveDayForecastByCoordinates(latitude,longitude,
                new WeatherManager.ForecastHandler() {
                    @Override
                    public void onReceivedForecast(WeatherManager manager, Forecast forecast) {
                        // Handle forecast

                        List<Double> list = new ArrayList<>();
                        for (int i = 0; i < 5; i++) {
                            long timestamp = forecast.getTimestampByIndex(i+3);
                            Weather weatherForTimestamp = forecast.getWeatherForTimestamp(timestamp);
                            Temperature tempMini = weatherForTimestamp.getTemperature().getMinimum();
                            double temperatureInCelcius = tempMini.getValue(TemperatureUnit.CELCIUS);
                            list.add(temperatureInCelcius);
                            Log.v("Weather", "" +weatherForTimestamp.getWind().getSpeed());
                            Log.v("Weather", "Température mini : " +  " "+ list.get(i));
                        }
                        int  minIndex = list.indexOf(Collections.min(list));
                        Log.v("Weather MINI", "Température mini : " + list.get(minIndex));
                        //Toast.makeText(context, "Température mini: " + list.get(minIndex), Toast.LENGTH_LONG).show();
                        temp =  list.get(minIndex);
                        Log.v("WeatherClass", temp + "");




                    }

                    @Override
                    public void onFailedToReceiveForecast(WeatherManager manager) {
                        Log.v("TAG",  " ERREUR");
                        temp = -1000000;

                    }
                }
        );
        return temp;
    }



}

答案 2 :(得分:0)

您的代码运行:

  1. 创建获取数据的请求。
  2. 返回温度= 0.0
  3. 从服务器接收数据并更新温度。

因此,您运行代码将获得返回值= 0.0。

要解决此问题,您可以执行以下操作:

  1. 创建新界面。 IResult.java

    public interface IResult {
        void onResult(double temp);
    }
    
  2. 更新getTemp方法:

     public static void getTemp(double latitude, double longitude, final IResult callback){
            String OPEN_WEATHER_MAP_API = "xxxxxxxxx";
            new WeatherManager(OPEN_WEATHER_MAP_API)
            .getFiveDayForecastByCoordinates(latitude,longitude,
                    new WeatherManager.ForecastHandler() {
                        @Override
                        public void onReceivedForecast(WeatherManager manager, Forecast forecast) {
                            // Handle forecast
    
                            List<Double> list = new ArrayList<>();
                            for (int i = 0; i < 5; i++) {
                                long timestamp = forecast.getTimestampByIndex(i+3);
                                Weather weatherForTimestamp = forecast.getWeatherForTimestamp(timestamp);
                                Temperature tempMini = weatherForTimestamp.getTemperature().getMinimum();
                                double temperatureInCelcius = tempMini.getValue(TemperatureUnit.CELCIUS);
                                list.add(temperatureInCelcius);
                                Log.v("Weather", "" +weatherForTimestamp.getWind().getSpeed());
                                Log.v("Weather", "Température mini : " +  " "+ list.get(i));
                            }
                            int  minIndex = list.indexOf(Collections.min(list));
                            Log.v("Weather MINI", "Température mini : " + list.get(minIndex));
                            //Toast.makeText(context, "Température mini: " + list.get(minIndex), Toast.LENGTH_LONG).show();
                            temp =  list.get(minIndex);
                            Log.v("WeatherClass", temp + "");
                            callback.onResult(temp);
    
    
    
                        }
    
                        @Override
                        public void onFailedToReceiveForecast(WeatherManager manager) {
                            Log.v("TAG",  " ERREUR");
                            temp = -1000000;
                            callback.onResult(temp);
    
                        }
                    }
            );
        }
    
  3. 使用

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WeatherClass.getTemp(32.32, 322.3, new IResult() {
            @Override
            public void onResult(double temp) {
                //result here
            }
        });
    }
    

更新:因为是第一次启动,所以此方法在更新温度之前返回0.0(步骤2返回温度,步骤3更新温度),因此您将获得0.0。第二次启动,此方法将返回第一次启动的结果(因为它在更新temp之前返回temp),因此您可能认为它是正确的(但不正确,返回值是第一次启动的数据)。