使用异步HTTP请求触发活动中的onSuccess

时间:2013-04-17 04:07:49

标签: android asynchronous

我正在编写一个简单的天气应用程序,以便了解如何为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()事件何时从我的主要活动中触发?

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:0)

使用侦听器 - 在开始下载时,使用downloadComplete方法将Darksky类传递给对象。在onSuccess中调用它,并将结果传递给它。调用downloadComplete时更新UI。