显示没有互联网连接的最近的已知天气状态

时间:2019-01-31 09:58:59

标签: java android

我使用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();
                               }
                           }

                )

        );
    }

}

2 个答案:

答案 0 :(得分:4)

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

因为您认为像位置服务存储的最后一个已知位置,Android是否会与位置服务的数据相同而不保留全部信息。

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

答案 1 :(得分:2)

您可以将数据保存在sqlite中,并检查是否没有Internet连接预览数据库中的数据

PS:使用ROOM(这是由android dev团队创建的组件),它可以更轻松地管理您的数据库。这是ROOM库的链接: https://developer.android.com/topic/libraries/architecture/room