java - SwipeRefreshLayout + AsyncTask : Fail to refresh data

标签 java android android-asynctask swiperefreshlayout

我在我的应用程序中同时使用 SwipeRefreshLayout 和 AsyncTask。我知道如果我要使用 AsyncTask 两次,我将必须创建一个新实例才能运行所有内容。但是,当我按如下方式执行此操作时,进程会卡住并且数据不会刷新。滑动按钮永远持续下去。

这是Function.java:

public interface AsyncResponse {

    void processFinish(String output1, String output2, String output3, String output4, String output5, String output6,
                       String output7, String output8, String output9, String output10, String output11, String output12,
                       String output13, String output14, String output15, String output16, String output17, String output18,
                       String output19, String output20, String output21, String output22, String output23, String output24);
}

public static class placeIdTask extends AsyncTask<String, Void, JSONObject> {

    public AsyncResponse delegate = null; //Call back interface

    public placeIdTask(AsyncResponse asyncResponse) {
        delegate = asyncResponse; //Assigning call back interface through constructor
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject jsonWeather = null;
        try {
            jsonWeather = getWeatherJSON();
        } catch (Exception e) {
            Log.d("Error", "Cannot process JSON results", e);
        }
        return jsonWeather;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        try {
            if (json != null) {
                ...... // more code
                delegate.processFinish(description, temperature, humidity, windspeed, updatedOn, iconText,
                        first_hour_icon, first_hour_text, first_hour_temperature,
                        third_hour_icon, third_hour_text, third_hour_temperature,
                        ninth_hour_icon, ninth_hour_text, ninth_hour_temperature,
                        first_day_icon, first_day_text, first_day_temperature,
                        second_day_icon, second_day_text, second_day_temperature,
                        third_day_icon, third_day_text, third_day_temperature);

            }
        } catch (JSONException e) {
            Log.e("Json Procession failed", e.toString());
        }
    }
}
public static JSONObject getWeatherJSON() { ... } // to retrieve data from the Internet

这是MainActivity.java:

... get everything imported

public class MainActivity extends AppCompatActivity {

... Variables declared

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");

        ...use findviewbyid to get things working

        final Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
            public void processFinish(String weather_description, String weather_temperature, String weather_humidity, String weather_windspeed, String weather_updatedOn, String weather_iconText,
                                      String weather_firstHourIcon, String weather_firstHourText, String weather_firstHourTemperature,
                                      String weather_thirdHourIcon, String weather_thirdHourText, String weather_thirdHourTemperature,
                                      String weather_ninthHourIcon, String weather_ninthHourText, String weather_ninthHourTemperature,
                                      String weather_firstDayIcon, String weather_firstDayText, String weather_firstDayTemperature,
                                      String weather_secondDayIcon, String weather_secondDayText, String weather_secondDayTemperature,
                                      String weather_thirdDayIcon, String weather_thirdDayText, String weather_thirdDayTemperature) {

                updatedField.setText(weather_updatedOn);
                detailsField.setText(weather_description);
                currentTemperatureField.setText(weather_temperature);
                humidity_field.setText("Humidity: " + weather_humidity);
                windspeed_field.setText("Wind Speed: " + weather_windspeed);
                firstHourText.setText(weather_firstHourText);
                thirdHourText.setText(weather_thirdHourText);
                ninthHourText.setText(weather_ninthHourText);
                firstHourTemperature.setText(weather_firstHourTemperature);
                thirdHourTemperature.setText(weather_thirdHourTemperature);
                ninthHourTemperature.setText(weather_ninthHourTemperature);
                firstDayText.setText(weather_firstDayText);
                secondDayText.setText(weather_secondDayText);
                thirdDayText.setText(weather_thirdDayText);
                firstDayTemperature.setText(weather_firstDayTemperature);
                secondDayTemperature.setText(weather_secondDayTemperature);
                thirdDayTemperature.setText(weather_thirdDayTemperature);
                weatherIcon.setText(Html.fromHtml(weather_iconText));
                firstHourIcon.setText(Html.fromHtml(weather_firstHourIcon));
                thirdHourIcon.setText(Html.fromHtml(weather_thirdHourIcon));
                ninthHourIcon.setText(Html.fromHtml(weather_ninthHourIcon));
                firstDayIcon.setText(Html.fromHtml(weather_firstDayIcon));
                secondDayIcon.setText(Html.fromHtml(weather_secondDayIcon));
                thirdDayIcon.setText(Html.fromHtml(weather_thirdDayIcon));
            }
        });

        asyncTask.execute();

        refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh_layout);
        scrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Function.placeIdTask asyncTask2 =new Function.placeIdTask(new Function.AsyncResponse() {
                    public void processFinish(String weather_description, String weather_temperature, String weather_humidity, String weather_windspeed, String weather_updatedOn, String weather_iconText,
                                              String weather_firstHourIcon, String weather_firstHourText, String weather_firstHourTemperature,
                                              String weather_thirdHourIcon, String weather_thirdHourText, String weather_thirdHourTemperature,
                                              String weather_ninthHourIcon, String weather_ninthHourText, String weather_ninthHourTemperature,
                                              String weather_firstDayIcon, String weather_firstDayText, String weather_firstDayTemperature,
                                              String weather_secondDayIcon, String weather_secondDayText, String weather_secondDayTemperature,
                                              String weather_thirdDayIcon, String weather_thirdDayText, String weather_thirdDayTemperature) {

                        updatedField.setText(weather_updatedOn);
                        detailsField.setText(weather_description);
                        currentTemperatureField.setText(weather_temperature);
                        humidity_field.setText("Humidity: " + weather_humidity);
                        windspeed_field.setText("Wind Speed: " + weather_windspeed);
                        firstHourText.setText(weather_firstHourText);
                        thirdHourText.setText(weather_thirdHourText);
                        ninthHourText.setText(weather_ninthHourText);
                        firstHourTemperature.setText(weather_firstHourTemperature);
                        thirdHourTemperature.setText(weather_thirdHourTemperature);
                        ninthHourTemperature.setText(weather_ninthHourTemperature);
                        firstDayText.setText(weather_firstDayText);
                        secondDayText.setText(weather_secondDayText);
                        thirdDayText.setText(weather_thirdDayText);
                        firstDayTemperature.setText(weather_firstDayTemperature);
                        secondDayTemperature.setText(weather_secondDayTemperature);
                        thirdDayTemperature.setText(weather_thirdDayTemperature);
                        weatherIcon.setText(Html.fromHtml(weather_iconText));
                        firstHourIcon.setText(Html.fromHtml(weather_firstHourIcon));
                        thirdHourIcon.setText(Html.fromHtml(weather_thirdHourIcon));
                        ninthHourIcon.setText(Html.fromHtml(weather_ninthHourIcon));
                        firstDayIcon.setText(Html.fromHtml(weather_firstDayIcon));
                        secondDayIcon.setText(Html.fromHtml(weather_secondDayIcon));
                        thirdDayIcon.setText(Html.fromHtml(weather_thirdDayIcon));
                    }
                });

                asyncTask2.execute();
                refreshLayout.setRefreshing(true);

            }
        });

    }
}

我是 SwipeRefreshLayout 以及 java 和 Android 的新手,发现很难理解该文档。谁能帮我吗?提前致谢。

最佳答案

在processFinish(..)内部执行refreshLayout.setRefreshing(false)

您还可以在一个函数本身中创建 asynctask,这样就不必编写两次实例化 asnyctask 的代码。

还可以通过使用日志或使用调试器来调试您的代码并告诉我。

关于java - SwipeRefreshLayout + AsyncTask : Fail to refresh data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341300/

相关文章:

JavaFX 在 FXML 文档中编辑 WebView

java - 我正在构建一个 REST 客户端来访问需要授权 header 的服务,但我不知道如何实现它

Android 许可证检查直接进入 applicationError(...)

android - 如何更新android sqlite数据库中的新行

android - 无法在对象上找到参数 [目录 'libs'] 的方法 compile()

android - 进度对话框显示太晚

java - 使用 Jackson JSON 查找字段类型

java - ArrayList<HashMap<String, String>> 无法正确转换为 API9 的 JSONArray

Android ListFragment 无法从异步任务填充

Android:当发生其他一些 ContentProvider 操作/sqlite 写入时,AsyncTask 中的 ContentProvider 请求会延迟