Android-第一行代码CoolWeather案例实战

                                        CoolWeather仿写

 

环境信息

           AndroidStudio 3.2      JDK1.8      运行与Android7.1 (项目本身不需要申请权限)

准备工作

           1 新建项目     需要添加的四个库(均是到目前为止最新的 库  添加到build.gradle )

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'org.litepal.android:core:2.0.0'

           2 申请和风天气的key  http://www.heweather.com/

                邮箱免费注册➡ 登陆➡控制台

Android-第一行代码CoolWeather案例实战_第1张图片

 

项目编写

                     1  定义各种包和文件

Android-第一行代码CoolWeather案例实战_第2张图片

 

                     2   编写dbsql 下的 省市县 实体类 并 配置 litepal库

 

Android-第一行代码CoolWeather案例实战_第3张图片

 

                  3   设置 litepal.xml 映射文件 并编写 Util包下 网络请求 方法

Android-第一行代码CoolWeather案例实战_第4张图片

 

                  4  Util包下新建Utility类 解析和保存网络获取的数据 

                              (因为上面图片都可以看完代码,所以不再附上代码)

                      (这里的代码较多 ,图片显示不完全,所以下面会附上代码)

Android-第一行代码CoolWeather案例实战_第5张图片

public class Utility {
    //解析和处理服务器返回的省级数据
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    //解析和处理服务器返回的市级数据
    public static boolean handleCityResponse(String response, int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObject = allCities.getJSONObject(i);
                    City city = new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    //解析和处理服务器返回的县级数据
    public static boolean handleCountyResponse(String response, int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObject = allCounties.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
   

}

 

                        5   下面就开始 编写界面了 Choose_area.xml

Android-第一行代码CoolWeather案例实战_第6张图片

 

                           6  使用碎片加载布局

Android-第一行代码CoolWeather案例实战_第7张图片

public class ChooseAreaFragment extends Fragment {
    public static final int LEVEL_PROVINCE = 0;
    public static final int LEVEL_CITY = 1;
    public static final int LEVEL_COUNTY = 2;
    private ProgressDialog progressDialog;
    private ArrayAdapter adapter;
    private List dataList = new ArrayList<>();
    //省列表
    private List provinceList;
    //市列表
    private List cityList;
    //县列表
    private List countyList;
    //选中的省份
    private Province selectedProvince;
    //选中的城市
    private City selectedCity;
    //当前选中的级别
    private int currentLevel;

    private TextView titleText;
    private Button backButton;
    private ListView listView;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.choose_area, container, false);
        initView();
        adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, dataList);
        listView.setAdapter(adapter);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
        //需要写上这句话,来创建数据库,要不然会报空指针
        Connector.getDatabase();
       //列表监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {          
             //如果选择的是省
                if (currentLevel == LEVEL_PROVINCE) {
                    selectedProvince = provinceList.get(position);
                    queryCities();
                    //如果选择的是市
                } else if (currentLevel == LEVEL_CITY) {
                    selectedCity = cityList.get(position);
                    queryCounties();
                } 
            }
        });
        //回退按钮监听器
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentLevel == LEVEL_COUNTY) {
                    queryCities();
                } else if (currentLevel == LEVEL_CITY) {
                    queryProvinces();
                }
            }
        });
        //调用 下面的方法
        queryProvinces();
    }

    // 查询全国所有的省,优先从数据库查询,如果没有查询到再去服务器上查询
    private void queryProvinces() {
        //设置 显示标题为中国
        titleText.setText("中国");
        //设置回退按钮隐藏
        backButton.setVisibility(View.GONE);
        //查询 省实体类的数据 
        provinceList = LitePal.findAll(Province.class);
        //判断是不是有数据 如果有
        if (provinceList.size() > 0) {
            //把要显示在listview的数据列表清空
            dataList.clear();
            //遍历省数据
            for (Province province : provinceList) {
                //把数据加到 要显示在listview的数据列表 上
                dataList.add(province.getProvinceName());
            }
            //通知数据更新
            adapter.notifyDataSetChanged();
            //设置listview 默认选择第一个
            listView.setSelection(0);
            // flag  设置为省    为回退做flag
            currentLevel = LEVEL_PROVINCE;
            //如果没有数据
        } else {
            //去服务器获取数据
            String address = "http://guolin.tech/api/china";
            queryFromServier(address, "province");
        }
    }

    //查询全国所有的市,优先从数据库查询,如果没有查询到再去服务器上查询
    private void queryCities() {
        titleText.setText(selectedProvince.getProvinceName());
        backButton.setVisibility(View.VISIBLE);
        cityList = LitePal.where("provinceid=?", String.valueOf(selectedProvince.getId())).find(City.class);
        if (cityList.size() > 0) {
            dataList.clear();
            for (City city : cityList) {
                dataList.add(city.getCityName());
            }
            adapter.notifyDataSetChanged();

            listView.setSelection(0);
            currentLevel = LEVEL_CITY;

        } else {
            int provinceCode = selectedProvince.getProvinceCode();
            String address = "http://guolin.tech/api/china/" + provinceCode;
            queryFromServier(address, "city");
        }
    }

    // 查询全国所有的县,优先从数据库查询,如果没有查询到再去服务器上查询
    private void queryCounties() {
        titleText.setText(selectedCity.getCityName());
        backButton.setVisibility(View.VISIBLE);
        countyList = LitePal.where("cityid=?", String.valueOf(selectedCity.getId())).find(County.class);
        if (countyList.size() > 0) {
            dataList.clear();
            for (County county : countyList) {
                dataList.add(county.getCountyName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            currentLevel = LEVEL_COUNTY;
        } else {
            int cityCode = selectedCity.getCityCode();
            int provinceCode = selectedProvince.getProvinceCode();
            String address = "http://guolin.tech/api/china/" + provinceCode + "/" + cityCode;
            queryFromServier(address, "county");
        }
    }

    // 根据传入的地址和类型 从服务器查询省市县信息数据
    private void queryFromServier(String address, final String type) {
        //显示对话框
        showProgressDialog();
        HttpUtil.sendOkHttpRequest(address, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //把获取到的数据设置为字符串
                String responseText = response.body().string();
                //设置结果
                boolean result = false;
                
                if ("province".equals(type)) {
                    //返回的省数据处理结果
                    result = Utility.handleProvinceResponse(responseText);
                } else if ("city".equals(type)) {
                 //返回的市数据处理结果
                    result = Utility.handleCityResponse(responseText, selectedProvince.getId());
                } else if ("county".equals(type)) {
                          //返回的县数据处理结果
                    result = Utility.handleCountyResponse(responseText, selectedCity.getId());
                }
                //根据返回的数据结果更新数据
                if (result) {
                    //获取主线程(UI必须在主线程更新)
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            closeProgressDialog();
                            if ("province".equals(type)) {
                                queryProvinces();
                            } else if ("city".equals(type)) {
                                queryCities();
                            } else if ("county".equals(type)) {
                                queryCounties();
                            }
                        }
                    });
                }


            }

            @Override
            public void onFailure(Call call, IOException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        closeProgressDialog();
                        Toast.makeText(getContext(), "加载失败", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("正在加载...");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    private void closeProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    private void initView() {
        titleText = view.findViewById(R.id.title_text);
        backButton = view.findViewById(R.id.back_button);
        listView = view.findViewById(R.id.list_view);
    }


}

                             7   在 activity_main.xml 加载碎片




    


                            8 现在就可以运行出现这样的结果

                                                            当然了  回退的图标需要自己找

 

                      9   既然选择城市的界面已经出来 那就应该点击县 进入天气界面

                                         在上面的  ChooseAreaFragment 碎片中已经定义了 listview 的点击事件具体处理方法不过只有点击了                                                省和点击了市的    现在要添加点击了县的  

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                    //点击了省
                if (currentLevel == LEVEL_PROVINCE) {
                    selectedProvince = provinceList.get(position);
                    queryCities();
                    //点击了市
                } else if (currentLevel == LEVEL_CITY) {
                    selectedCity = cityList.get(position);
                    queryCounties();
                }
                //点击了县
                else if (currentLevel == LEVEL_COUNTY) {
                    //获取县的实体类中的 WeatherId
                    String weatherId = countyList.get(position).getWeatherId();
                    //如果这是在 MainActivity 的xml布局中的碎片
                    if (getActivity() instanceof MainActivity) {
                        //用 Intent 传值 直接跳转到 WeatherActivity 活动
                        Intent intent = new Intent(getActivity(), WeatherActivity.class);
                        intent.putExtra("weather_id", weatherId);
                        startActivity(intent);
                        //关闭这个活动
                        getActivity().finish();
                       // 如果这是在 WeatherActivity的xml布局中的碎片 也就是抽屉布局中的
                    } else if (getActivity() instanceof WeatherActivity) {
                        //获得 活动实例
                        WeatherActivity activity = (WeatherActivity) getActivity();
                        //使用 SharedPreferences 把 weather_id 储存起来
                        SharedPreferences.Editor editor =PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
                        editor.putString("weather_id", weatherId);
                        editor.apply();

                        //关闭抽屉
                        activity.drawerLayout.closeDrawers();
                        //显示刷新图标
                        activity.swipeRefresh.setRefreshing(true);
                        //请求天气(下面会有)
                        activity.requestWeather(weatherId);
                    }

                }
            }
        });

 

 

                        10  编写天气界面 书上是 分步写的  ,所以新建文件比较多,嫌麻烦就写在一个里

 



  
    
    
    
            
       
              
            
                
                      
                    
                    
                    
                    
                    
                
            
        
      
        
    

                                                                                            这五个布局就不写了很简单

                                  11    接下来就是  WeatherActivity.java

                                                            在此之前要定义一个工具方法 用来将返回的 JSON 数据 解析称实体类

Android-第一行代码CoolWeather案例实战_第8张图片

 

                                                然后再进行WeatherActivity.java

Android-第一行代码CoolWeather案例实战_第9张图片

public class WeatherActivity extends AppCompatActivity {
    private ScrollView weatherLayout;private TextView titleCity;
    private TextView titleUpdateTime;private TextView degreeText;
    private TextView weatherInfoText;private LinearLayout forecastLayout;
    private TextView aqiText;private TextView pm25Text;
    private TextView comfortText;private TextView carWashText;
    private TextView sportText;private ImageView backgroundImg;
    public SwipeRefreshLayout swipeRefresh;public DrawerLayout drawerLayout;
    private Button navButton;private Intent intent;
    private String mWeatherId;
    //设置沉浸式
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸模式
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_weather);
        //调用初始化控件方法
        initView();
    }
    //获取控件
    private void initView() {
        weatherLayout = findViewById(R.id.weather_layout);
        titleCity = findViewById(R.id.title_city);
        titleUpdateTime = findViewById(R.id.title_update_time);
        degreeText = findViewById(R.id.degree_text);
        weatherInfoText = findViewById(R.id.weather_info_text);
        forecastLayout = findViewById(R.id.forecast_layout);
        aqiText = findViewById(R.id.aqi_text);
        pm25Text = findViewById(R.id.pm25_text);
        comfortText = findViewById(R.id.comfort_text);
        carWashText = findViewById(R.id.car_wash_text);
        sportText = findViewById(R.id.sport_text);
        backgroundImg = findViewById(R.id.background_img);
        swipeRefresh = findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        drawerLayout = findViewById(R.id.drawer_layout);
        navButton = findViewById(R.id.nav_button);
        navButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });


        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String weatherString = prefs.getString("weather", null);
        String bing_pic = prefs.getString("bing_pic", null);
        if (bing_pic != null) {
            Glide.with(this).load(bing_pic).into(backgroundImg);
        } else {
            loadBingPic();
        }


        if (weatherString != null) {
            //有缓存时直接解析天气
            Weather weather = Utility.handleWeatherResponse(weatherString);
            mWeatherId = weather.basic.weatherId;
            ShowWeatherInfo(weather);
        } else {
            //没有缓存信息去服务器查询信息
            mWeatherId = getIntent().getStringExtra("weather_id");
            weatherLayout.setVisibility(View.INVISIBLE);
            requestWeather(mWeatherId);
        }

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                String weather_id = prefs.getString("weather_id", null);
                requestWeather(weather_id);
            }
        });
    }
    //加载每日一图
    private void loadBingPic() {
        String requestBinfPic = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequest(requestBinfPic, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String bingpic = response.body().string();
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
                editor.putString("bing_pic", bingpic);
                editor.apply();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(WeatherActivity.this).load(bingpic).into(backgroundImg);

                    }
                });

            }

            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
        });
    }
    //根据天气 ID 请求城市天气
    public void requestWeather(final String weatherId) {
        String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId +
                "&key=d4892ac004c84b498d27455a89623852";

      //  http://guolin.tech/api/weather?cityid=CN101080507&key=d4892ac004c84b498d27455a89623852
        HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseText = response.body().string();
                final Weather weather = Utility.handleWeatherResponse(responseText);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (weather != null && "ok".equals(weather.status)) {
                            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
                            editor.putString("weather", responseText);
                            editor.apply();
                            ShowWeatherInfo(weather);
                        } else {
                            Toast.makeText(WeatherActivity.this, "获取天气信息失败"
                                    , Toast.LENGTH_SHORT).show();
                        }
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(WeatherActivity.this, "获取天气信息失败"
                                , Toast.LENGTH_SHORT).show();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        });
    }
    // 处理并显示Weather 实体类中的数据
    private void ShowWeatherInfo(Weather weather) {
        if (weather != null && "ok".equals(weather.status)) {
            String cityName = weather.basic.cityName;
            String upadteTime = weather.basic.update.updateTime.split(" ")[1];
            String degrss = weather.now.temperature + "℃";
            String weatherinfo = weather.now.more.info;

            titleCity.setText(cityName);
            titleUpdateTime.setText(upadteTime);
            degreeText.setText(degrss);
            weatherInfoText.setText(weatherinfo);
            forecastLayout.removeAllViews();
            for (Forecast forecast : weather.forecastList) {
                View view = LayoutInflater.from(this).inflate(R.layout.forecast_item, forecastLayout, false);
                TextView dateText = view.findViewById(R.id.date_text);
                TextView infoText = view.findViewById(R.id.info_text);
                TextView maxText = view.findViewById(R.id.max_text);
                TextView minText = view.findViewById(R.id.min_text);
                dateText.setText(forecast.date);
                infoText.setText(forecast.more.info);
                maxText.setText(forecast.temperature.max);
                minText.setText(forecast.temperature.min);
                forecastLayout.addView(view);
            }

            if (weather.aqi != null) {
                aqiText.setText(weather.aqi.city.aqi);
                pm25Text.setText(weather.aqi.city.pm25);
            }
            String comfort = "舒适度 :\n" + weather.suggestion.comfort.info;
            String carwach = "洗车指数:\n" + weather.suggestion.carWash.info;
            String sport = "运动建议:\n" + weather.suggestion.sport.info;

            comfortText.setText(comfort);
            carWashText.setText(carwach);
            sportText.setText(sport);
            weatherLayout.setVisibility(View.VISIBLE);
            
            intent = new Intent(this, AutoUpdateService.class);
            startService(intent);

        } else {
            Toast.makeText(WeatherActivity.this, "获取天气信息失败"
                    , Toast.LENGTH_SHORT).show();
        }

    }
    @Override
    protected void onPause() {
        super.onPause();
        stopService(intent);
    }
}

                        12   在Service包下 新建 Service

Android-第一行代码CoolWeather案例实战_第10张图片

public class AutoUpdateService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        updateWeather();
        updateBingPic();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int anHOur = 3*60 * 1000;
        long triggerAtTime = SystemClock.elapsedRealtime() + anHOur;
        Intent i = new Intent(this, AutoUpdateService.class);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        manager.cancel(pi);
        return super.onStartCommand(intent, flags, startId);
    }

    //更新每日一图 保存到 SharedPreferences
    private void updateBingPic() {
        String requestBinfPic = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequest(requestBinfPic, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String bingpic = response.body().string();
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                editor.putString("bing_pic", bingpic);
                editor.apply();
            }

            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
        });
    }

    //更新天气  保存到SharedPreferences
    private void updateWeather() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String weatherString = prefs.getString("weather", null);
        if (weatherString != null) {
            //有缓存时直接解析天气数据
            Weather weather = Utility.handleWeatherResponse(weatherString);

            String weatherId = weather.basic.weatherId;

            String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId +
                    "&key=d4892ac004c84b498d27455a89623852";

            HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    String responseText = response.body().string();

                    Weather weather = Utility.handleWeatherResponse(responseText);

                    if (weather != null && "ok".equals(weather.status)) {
                        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                        editor.putString("weather", responseText);
                        editor.apply();

                    }
                }

                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

                   但是每次运行都会出现省市县的选择 所以需要设置 MainActivity.java

Android-第一行代码CoolWeather案例实战_第11张图片

                                                      这样 基本项目就完成了

还可以做的事

             可以 在Service 中 放   Notification  让数据实时显示到状态栏

              增加 设置界面  是否显示   Notification

              增加一个 悬浮按钮  显示 选择过的县   不用再来回切换县

              。。。

 

 

你可能感兴趣的:(Android-第一行代码CoolWeather案例实战)