我使用java和android studio构建了一个天气应用程序,我使用openweathermap API返回了天气数据

另外,我使用了Retrofit库

我想要的是
当没有互联网连接时,我想显示最新的天气状态,而不是页面中的进度条

你能帮我吗?

我的天气片段类代码是这样

public class TodayWeatherFragment extends Fragment {


    ImageView img_weather;
    TextView txt_city_name, txt_temp, txt_discription, txt_date_time, txt_wind,txt_pressure , txt_humiditty,
            txt_sunrise,txt_sunset, txt_geo_coords;
    LinearLayout weather_panel;
    ProgressBar loading;


    CompositeDisposable compositeDisposable;
    IOpenWeatherClass mService ;


    static TodayWeatherFragment instance;

    public static TodayWeatherFragment getInstance() {
        if(instance == null)
            instance = new TodayWeatherFragment();
        return instance;
    }

    public TodayWeatherFragment() {

        compositeDisposable = new CompositeDisposable();
        Retrofit retrofit =  RetrofitClient.getInstance();

        mService=retrofit.create(IOpenWeatherClass.class);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View itemView = inflater.inflate(R.layout.fragment_today_weather, container, false);

        img_weather = (ImageView) itemView.findViewById(R.id.img_weather);
        txt_city_name = (TextView) itemView.findViewById(R.id.txt_city_name);
        txt_temp = (TextView) itemView.findViewById(R.id.txt_temp);
        txt_discription = (TextView) itemView.findViewById(R.id.txt_discription);
        txt_date_time = (TextView) itemView.findViewById(R.id.txt_date_time);
        txt_wind = (TextView) itemView.findViewById(R.id.txt_wind);
        txt_pressure = (TextView) itemView.findViewById(R.id.txt_pressure);
        txt_humiditty = (TextView) itemView.findViewById(R.id.txt_humiditty);
        txt_sunrise = (TextView) itemView.findViewById(R.id.txt_sunrise);
        txt_sunset = (TextView) itemView.findViewById(R.id.txt_sunset);
        txt_geo_coords = (TextView) itemView.findViewById(R.id.txt_geo_coords);

        weather_panel = (LinearLayout)itemView.findViewById(R.id.weather_panel);

        loading = (ProgressBar) itemView.findViewById(R.id.loading);

        getWeatherInformation();


        return itemView;
    }

    private void getWeatherInformation() {

        compositeDisposable.add(mService.getWeatherByLatLng(String.valueOf(Common.current_location.getLatitude()),
                String.valueOf(Common.current_location.getLatitude()),
                Common.APP_ID,
                "metric").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<WeatherResult>() {
                               @Override
                               public void accept(WeatherResult weatherResult) throws Exception {

                                   //Load image
                                   Picasso.get().load(new StringBuilder("https://openweathermap.org/img/w/")
                                           .append(weatherResult.getWeather().get(0).getIcon())
                                   .append(".png").toString()).into(img_weather);


                                   //load information

                                   txt_city_name.setText(weatherResult.getName());
                                   txt_discription.setText(new StringBuilder("Weather in ")
                                   .append(weatherResult.getName()).toString());
                                   txt_temp.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getTemp()))
                                   .append("C").toString());
                                   txt_date_time.setText(Common.convertUnixToDate(weatherResult.getDt()));
                                   txt_pressure.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getPressure()))
                                           .append("hpa").toString());
                                   txt_humiditty.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getHumidity()))
                                           .append("%").toString());
                                   txt_sunrise.setText(Common.convertUnixToHour(weatherResult.getSys().getSunrise()));
                                   txt_sunset.setText(Common.convertUnixToHour(weatherResult.getSys().getSunset()));
                                   txt_geo_coords.setText(new StringBuilder("[").append(weatherResult.getCoord().toString())
                                           .append("]").toString());

                                   //display panel

                                   weather_panel.setVisibility(View.VISIBLE);
                                   loading.setVisibility(View.GONE);






                               }

                           }, new Consumer<Throwable>() {
                               @Override
                               public void accept(Throwable throwable) throws Exception {
                                   Toast.makeText(getActivity(), ""+throwable.getMessage(), Toast.LENGTH_SHORT).show();
                               }
                           }

                )

        );
    }

}

最佳答案

您应该最后知道应用程序是否以文件,文件,SQLite数据库,共享首选项等任何格式来存储数据。

因为您认为像位置服务存储的最后一个已知位置Android并不完整保留数据是否与位置服务相同。

因此,您应该保留带有自己的应用程序的数据是否带有时间戳,以便在无法连接互联网时可以通过与当前日期时间戳匹配来从已保存的数据中显示数据。

关于java - 显示没有互联网连接的最近的已知天气状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54457909/

10-09 03:02