1

我正在编写一个简单的天气应用程序,以学习如何为 Android 编程,但是我遇到了困难。

目前,应用程序获取用户 GPS 位置,然后向 API 发送请求以获取天气信息。问题是,我已经将这些 API 调用抽象为另一个类Darksky,它们异步运行。

当我执行以下操作时,这会成为一个问题:weeklyForecast = darksky.getWeeklyForecast();. 因为请求在getWeeklyForecast()方法中异步运行,所以weeklyForecast会返回为空,因为方法在得到 API 响应之前返回。

这是任何有兴趣的人的方法:

public ArrayList<Forecast> getWeeklyForecast() {
    // Clear before inserting new data
    weeklyForecast.clear();

    client.get(BASE_URL + API_KEY + "/" + latitude + "," + longitude + "?units=uk&exclude=hourly%2Calerts%2Cflags", null, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            try {
                JSONObject root = new JSONObject(response);
                JSONObject daily = root.getJSONObject("daily");
                JSONArray forecasts = daily.getJSONArray("data");

                // Loop through every day
                for (int i = 0; i < forecasts.length(); i++) {
                    JSONObject dayObject = forecasts.getJSONObject(i);

                    weeklyForecast.add(new Forecast(
                        dayObject.getInt("time"),
                        dayObject.getString("summary"),
                        dayObject.getString("icon"),
                        dayObject.getDouble("temperatureMin"),
                        dayObject.getDouble("temperatureMax"),
                        dayObject.getDouble("windSpeed"),
                        dayObject.getDouble("humidity")
                    ));
                }
            } catch(Exception e) {
                // TODO: handle   
            }
        }
    });

    return weeklyForecast;
}

如何检测 HTTP 请求的onSuccess()事件何时从我的主要活动中触发?

4

2 回答 2

1

下载完成后使用接口获取回调,如下所示。

class Darksky{

    interface updateUI{
        void onFinishedDownload(ArrayList<Forecast> array);
    }

    public ArrayList<Forecast> getWeeklyForecast() {
        // Clear before inserting new data
        weeklyForecast.clear();

        client.get(BASE_URL + API_KEY + "/" + latitude + "," + longitude + "?units=uk&exclude=hourly%2Calerts%2Cflags", null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject root = new JSONObject(response);
                    JSONObject daily = root.getJSONObject("daily");
                    JSONArray forecasts = daily.getJSONArray("data");

                    // Loop through every day
                    for (int i = 0; i < forecasts.length(); i++) {
                        JSONObject dayObject = forecasts.getJSONObject(i);

                        weeklyForecast.add(new Forecast(
                                dayObject.getInt("time"),
                                dayObject.getString("summary"),
                                dayObject.getString("icon"),
                                dayObject.getDouble("temperatureMin"),
                                dayObject.getDouble("temperatureMax"),
                                dayObject.getDouble("windSpeed"),
                                dayObject.getDouble("humidity")
                                ));
                    }
                } catch(Exception e) {
                    // TODO: handle   
                }

                updateUI.onFinishedDownload(weeklyForecast);
            }
        });

    }
}

在您的 Activity 或 UIClasses 中,您只需要实例化 updateUI 接口并使用 arraylist 传递:

@Override
public void onFinishedDownload(ArrayList<Forecast> array) {
    // Do something with your arraylist here.
}
于 2013-04-17T04:21:40.857 回答
0

使用监听器——当你开始下载时,通过 downloadComplete 方法向 Darksky 类传递一个对象。在 onSuccess 中调用它,并将结果传递给它。调用 downloadComplete 时更新 UI。

于 2013-04-17T04:11:00.930 回答