With a simple weather forecast app that quotes the wind api teaches you how to parse JSON data in Android development

tags: JSON  android

1. JSON format and JSON data character analysis method

First, we need to know the format of JSON. So what is the format of JSON?

{

“name”:“JY”,

“age”:22

}     

This is the simplest JSON format example. The JOSN format uses the key: value key value corresponding to write, key is a string, value is the value of the key, it has String, number, boolean, array and other types. Use "," to separate the keys.

There are generally three ways to parse traditional JSON in JAVA:

First you generate a JSON string, you can generate

public static String createJsonString(String key, Object value) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(key, value);
    return jsonObject.toString();
}

Generate JSON data as

1.A JavaBean,

2.A List array,

3.A List array of nested Maps

Here I recommend the first method (generated as a JavaBean), because in the project, you generally need to do some processing on the value of the key in the JSON data. At this time, the benefits of JavaBean are reflected. Compared with the other two The use of the method is more flexible.

2. GSON of JSON

There is a very easy-to-use plug-in in Android Studio, that is, GsonFormat, which can conveniently help us automatically generate JSON data strings into a JavaBean, and it is easy to parse with Gson.

After you install GsonFormat in android studio, the steps are as follows:

1. Select Generate in Code


   

   

2. Check

3. Copy the JSON data you get directly into it to generate the JavaBean you need with one click



Well, the basics have been finished. Let's talk about how to get the JSON data of the weather

Third, access to the weather forecast demo of the wind weather API

If you want to access the api, you need an app key. At this time, we need to go to the official website of Hefeng Weather to register an account. Generally, we choose an individual developer. The free api is enough for our test. Of course, if you are using more data for a company project, you can choose to register as an enterprise customer.



After the registration is completed, you need to go to the console to find your authentication key, because this key is required in the parameters of the api access



So, how should we access it? Don't worry, people have access to documents!


The place indicated by the arrow is the way to access the api. The parameters are described below. You can access it according to your needs, but you must have a key

Here, you can directly input this request address into the browser to see what the requested JSON data is. However, personally recommend using postman to view, because you don't need to change the format yourself, and automatically generate the format you want. Do you find it convenient?



Well, until now, we have obtained the JSON data of the weather we need, and the next step is how to generate it as a JavaBean in the demo and parse them.

Remember the GsonFormat plug-in for android studio that we mentioned earlier, yes, yes, now is the time to use it. Since I was requesting all free weather APIs, the code is a lot of very long, but it doesn’t matter, because it is not needed You write this code, after all, one-key generation, it will work, right?

public class WeatherBean {

    private List<HeWeather6Bean> HeWeather6;

    public List<HeWeather6Bean> getHeWeather6() {
        return HeWeather6;
    }

    public void setHeWeather6(List<HeWeather6Bean> HeWeather6) {
        this.HeWeather6 = HeWeather6;
    }

    public static class HeWeather6Bean {
        /**
                   * basic: {"cid":"CN101010100","location":"Beijing","parent_city":"Beijing","admin_area":"Beijing","cnty":"China","lat":"39.90498734 ","lon":"116.4052887","tz":"+8.00"}
         * update : {"loc":"2018-05-01 14:47","utc":"2018-05-01 06:47"}
         * status : ok
                   * now: {"cloud":"0","cond_code":"504","cond_txt":"Floating","fl":"18","hum":"26","pcpn":"0.0 ","pres":"1013","tmp":"21","vis":"10","wind_deg":"72","wind_dir":"Northeast Wind","wind_sc":"3" ,"wind_spd":"14"}
         * daily_forecast: [{"cond_code_d":"504","cond_code_n":"101","cond_txt_d":"Floating Dust","cond_txt_n":"Cloudy","date":"2018-05-01"," hum":"35","mr":"20:19","ms":"06:13","pcpn":"0.0","pop":"0","pres":"1015" ,"sr":"05:16","ss":"19:09","tmp_max":"24","tmp_min":"13","uv_index":"6","vis":" 20","wind_deg":"3","wind_dir":"North Wind","wind_sc":"4-5","wind_spd":"33"},{"cond_code_d":"101","cond_code_n ":"100","cond_txt_d":"Cloudy","cond_txt_n":"Sunny","date":"2018-05-02","hum":"18","mr":"21:19 ","ms":"06:49","pcpn":"0.0","pop":"0","pres":"1020","sr":"05:15","ss": "19:10","tmp_max":"24","tmp_min":"13","uv_index":"6","vis":"20","wind_deg":"210","wind_dir": "Southwest Wind","wind_sc":"3-4","wind_spd":"21"},{"cond_code_d":"100","cond_code_n":"100","cond_txt_d":" "," cond_txt_n":"Sunny","date":"2018-05-03","hum":"20","mr":"22:14","ms":"07:28","pcpn" :"0.0","pop":"0","pres":"1022","sr":"05:13","ss":"19:11","tmp_max":"24"," tmp_min":"13","uv_index":"7","vis":"20","wind_deg":"177","wind_dir":"Southwind","wind_sc":"1-2", "wind_spd":"4"}]
                   * lifestyle: [{"type":"comf","brf":"Comfortable","txt":"During the day is not too hot nor too cold, the wind is not strong, I believe you should be able to I feel relatively refreshed and comfortable."},{"type":"drsg","brf":"More comfortable","txt":"It is recommended to wear thin coats, cardigans, denim pants and other clothing. The elderly and infirm should be appropriate Add clothing, suitable for jackets, thin sweaters, etc."},{"type":"flu","brf":"Easier to send","txt":"Although the temperature is suitable, but the wind is relatively strong, it is still easier If you have a cold and have a weak constitution, please pay attention to proper protection."},{"type":"sport","brf":"more suitable","txt":"Overcast, suitable for various indoor and outdoor sports ."},{"Type":"trav","brf":"suitable","txt":"The weather is better, the wind is a bit stronger, but the temperature is suitable, and overall the weather is still good. Such weather is suitable for tourism , You can enjoy the scenery of nature."},{"type":"uv","brf":"The weakest","txt":"Weak UV radiation weather, no special protection is required. If it is outdoors for a long time , It is recommended to apply sunscreen skin care products with SPF between 8-12."},{"type":"cw","brf":"less suitable","txt":"less suitable for car washing, no rain in the future , The wind is strong, if you insist on scrubbing the car, you must be mentally prepared to be covered with dirt."},{"type":"air","brf":"Good","txt":"Meteorological conditions are good for the air Dilution, diffusion and removal of pollutants allow normal activities outdoors."}]
         */
        private BasicBean basic;
        private UpdateBean update;
        private String status;
        private NowBean now;
        private List<DailyForecastBean> daily_forecast;
        private List<LifestyleBean> lifestyle;

        public BasicBean getBasic() {
            return basic;
        }

        public void setBasic(BasicBean basic) {
            this.basic = basic;
        }

        public UpdateBean getUpdate() {
            return update;
        }

        public void setUpdate(UpdateBean update) {
            this.update = update;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public NowBean getNow() {
            return now;
        }

        public void setNow(NowBean now) {
            this.now = now;
        }

        public List<DailyForecastBean> getDaily_forecast() {
            return daily_forecast;
        }

        public void setDaily_forecast(List<DailyForecastBean> daily_forecast) {
            this.daily_forecast = daily_forecast;
        }

        public List<LifestyleBean> getLifestyle() {
            return lifestyle;
        }

        public void setLifestyle(List<LifestyleBean> lifestyle) {
            this.lifestyle = lifestyle;
        }

        public static class BasicBean {
            /**
             * cid : CN101010100
                           * location: Beijing
                           * parent_city: Beijing
                           * admin_area: Beijing
                           * cnty: China
             * lat : 39.90498734
             * lon : 116.4052887
             * tz : +8.00
             */

            private String cid;
            private String location;
            private String parent_city;
            private String admin_area;
            private String cnty;
            private String lat;
            private String lon;
            private String tz;

            public String getCid() {
                return cid;
            }

            public void setCid(String cid) {
                this.cid = cid;
            }

            public String getLocation() {
                return location;
            }

            public void setLocation(String location) {
                this.location = location;
            }

            public String getParent_city() {
                return parent_city;
            }

            public void setParent_city(String parent_city) {
                this.parent_city = parent_city;
            }

            public String getAdmin_area() {
                return admin_area;
            }

            public void setAdmin_area(String admin_area) {
                this.admin_area = admin_area;
            }

            public String getCnty() {
                return cnty;
            }

            public void setCnty(String cnty) {
                this.cnty = cnty;
            }

            public String getLat() {
                return lat;
            }

            public void setLat(String lat) {
                this.lat = lat;
            }

            public String getLon() {
                return lon;
            }

            public void setLon(String lon) {
                this.lon = lon;
            }

            public String getTz() {
                return tz;
            }

            public void setTz(String tz) {
                this.tz = tz;
            }
        }

        public static class UpdateBean {
            /**
             * loc : 2018-05-01 14:47
             * utc : 2018-05-01 06:47
             */

            private String loc;
            private String utc;

            public String getLoc() {
                return loc;
            }

            public void setLoc(String loc) {
                this.loc = loc;
            }

            public String getUtc() {
                return utc;
            }

            public void setUtc(String utc) {
                this.utc = utc;
            }
        }

        public static class NowBean {
            /**
             * cloud : 0
             * cond_code : 504
                           * cond_txt: floating dust
             * fl : 18
             * hum : 26
             * pcpn : 0.0
             * pres : 1013
             * tmp : 21
             * vis : 10
             * wind_deg : 72
                           * wind_dir: northeast wind
             * wind_sc : 3
             * wind_spd : 14
             */

            private String cloud;
            private String cond_code;
            private String cond_txt;
            private String fl;
            private String hum;
            private String pcpn;
            private String pres;
            private String tmp;
            private String vis;
            private String wind_deg;
            private String wind_dir;
            private String wind_sc;
            private String wind_spd;

            public String getCloud() {
                return cloud;
            }

            public void setCloud(String cloud) {
                this.cloud = cloud;
            }

            public String getCond_code() {
                return cond_code;
            }

            public void setCond_code(String cond_code) {
                this.cond_code = cond_code;
            }

            public String getCond_txt() {
                return cond_txt;
            }

            public void setCond_txt(String cond_txt) {
                this.cond_txt = cond_txt;
            }

            public String getFl() {
                return fl;
            }

            public void setFl(String fl) {
                this.fl = fl;
            }

            public String getHum() {
                return hum;
            }

            public void setHum(String hum) {
                this.hum = hum;
            }

            public String getPcpn() {
                return pcpn;
            }

            public void setPcpn(String pcpn) {
                this.pcpn = pcpn;
            }

            public String getPres() {
                return pres;
            }

            public void setPres(String pres) {
                this.pres = pres;
            }

            public String getTmp() {
                return tmp;
            }

            public void setTmp(String tmp) {
                this.tmp = tmp;
            }

            public String getVis() {
                return vis;
            }

            public void setVis(String vis) {
                this.vis = vis;
            }

            public String getWind_deg() {
                return wind_deg;
            }

            public void setWind_deg(String wind_deg) {
                this.wind_deg = wind_deg;
            }

            public String getWind_dir() {
                return wind_dir;
            }

            public void setWind_dir(String wind_dir) {
                this.wind_dir = wind_dir;
            }

            public String getWind_sc() {
                return wind_sc;
            }

            public void setWind_sc(String wind_sc) {
                this.wind_sc = wind_sc;
            }

            public String getWind_spd() {
                return wind_spd;
            }

            public void setWind_spd(String wind_spd) {
                this.wind_spd = wind_spd;
            }
        }

        public static class DailyForecastBean {
            /**
             * cond_code_d : 504
             * cond_code_n : 101
                           * cond_txt_d: floating dust
                           * cond_txt_n: cloudy
             * date : 2018-05-01
             * hum : 35
             * mr : 20:19
             * ms : 06:13
             * pcpn : 0.0
             * pop : 0
             * pres : 1015
             * sr : 05:16
             * ss : 19:09
             * tmp_max : 24
             * tmp_min : 13
             * uv_index : 6
             * vis : 20
             * wind_deg : 3
                           * wind_dir: north wind
             * wind_sc : 4-5
             * wind_spd : 33
             */

            private String cond_code_d;
            private String cond_code_n;
            private String cond_txt_d;
            private String cond_txt_n;
            private String date;
            private String hum;
            private String mr;
            private String ms;
            private String pcpn;
            private String pop;
            private String pres;
            private String sr;
            private String ss;
            private String tmp_max;
            private String tmp_min;
            private String uv_index;
            private String vis;
            private String wind_deg;
            private String wind_dir;
            private String wind_sc;
            private String wind_spd;

            public String getCond_code_d() {
                return cond_code_d;
            }

            public void setCond_code_d(String cond_code_d) {
                this.cond_code_d = cond_code_d;
            }

            public String getCond_code_n() {
                return cond_code_n;
            }

            public void setCond_code_n(String cond_code_n) {
                this.cond_code_n = cond_code_n;
            }

            public String getCond_txt_d() {
                return cond_txt_d;
            }

            public void setCond_txt_d(String cond_txt_d) {
                this.cond_txt_d = cond_txt_d;
            }

            public String getCond_txt_n() {
                return cond_txt_n;
            }

            public void setCond_txt_n(String cond_txt_n) {
                this.cond_txt_n = cond_txt_n;
            }

            public String getDate() {
                return date;
            }

            public void setDate(String date) {
                this.date = date;
            }

            public String getHum() {
                return hum;
            }

            public void setHum(String hum) {
                this.hum = hum;
            }

            public String getMr() {
                return mr;
            }

            public void setMr(String mr) {
                this.mr = mr;
            }

            public String getMs() {
                return ms;
            }

            public void setMs(String ms) {
                this.ms = ms;
            }

            public String getPcpn() {
                return pcpn;
            }

            public void setPcpn(String pcpn) {
                this.pcpn = pcpn;
            }

            public String getPop() {
                return pop;
            }

            public void setPop(String pop) {
                this.pop = pop;
            }

            public String getPres() {
                return pres;
            }

            public void setPres(String pres) {
                this.pres = pres;
            }

            public String getSr() {
                return sr;
            }

            public void setSr(String sr) {
                this.sr = sr;
            }

            public String getSs() {
                return ss;
            }

            public void setSs(String ss) {
                this.ss = ss;
            }

            public String getTmp_max() {
                return tmp_max;
            }

            public void setTmp_max(String tmp_max) {
                this.tmp_max = tmp_max;
            }

            public String getTmp_min() {
                return tmp_min;
            }

            public void setTmp_min(String tmp_min) {
                this.tmp_min = tmp_min;
            }

            public String getUv_index() {
                return uv_index;
            }

            public void setUv_index(String uv_index) {
                this.uv_index = uv_index;
            }

            public String getVis() {
                return vis;
            }

            public void setVis(String vis) {
                this.vis = vis;
            }

            public String getWind_deg() {
                return wind_deg;
            }

            public void setWind_deg(String wind_deg) {
                this.wind_deg = wind_deg;
            }

            public String getWind_dir() {
                return wind_dir;
            }

            public void setWind_dir(String wind_dir) {
                this.wind_dir = wind_dir;
            }

            public String getWind_sc() {
                return wind_sc;
            }

            public void setWind_sc(String wind_sc) {
                this.wind_sc = wind_sc;
            }

            public String getWind_spd() {
                return wind_spd;
            }

            public void setWind_spd(String wind_spd) {
                this.wind_spd = wind_spd;
            }
        }

        public static class LifestyleBean {
            /**
             * type : comf
                           * brf: comfortable
                           * txt: It is not too hot or too cold during the day, and the wind is not strong. I believe you should feel more refreshing and comfortable under such weather conditions.
             */

            private String type;
            private String brf;
            private String txt;

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }

            public String getBrf() {
                return brf;
            }

            public void setBrf(String brf) {
                this.brf = brf;
            }

            public String getTxt() {
                return txt;
            }

            public void setTxt(String txt) {
                this.txt = txt;
            }
        }
    }
}

With this JavaBean, it’s easy to work, just call the analysis where it needs to be called (PS: bloggers are lazy, too lazy to optimize the code. Obviously it can be done with butterknife, without having to knock so many hands on the code, It’s not necessary to do such a stupid thing. But this code has been done by the blogger a long time ago, so I didn’t modify and optimize it. This is also because I recently wanted to write a blog and suddenly thought of this demo and pulled it out.)

public class MainActivity extends AppCompatActivity {
    RequestQueue queue = null;
    EditText et_location;
    TextView tv_city, tv_date, tv_max_temp, tv_min_temp, tv_temp, tv_cond, tv_dir, tv_comf, tv_suggest,
            tv_pre_day1_maxtmp, tv_pre_day1_mintmp, tv_pre_day1_cond, tv_pre_day1_wind,
            tv_pre_day2_maxtmp, tv_pre_day2_mintmp, tv_pre_day2_cond, tv_pre_day2_wind,
            tv_pre_day3_maxtmp, tv_pre_day3_mintmp, tv_pre_day3_cond, tv_pre_day3_wind,
            title_pre, title_pre_day1, title_pre_day2, title_pre_day3;
    Editable location;
   LinearLayoutCompat ly_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //Transparent status bar
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                         //Transparent navigation bar
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main);
        ly_container= (LinearLayoutCompat) findViewById(R.id.ly_container);

        queue = Volley.newRequestQueue(this);
        et_location = (EditText) findViewById(R.id.et_location);
        tv_city = (TextView) findViewById(R.id.id_tv_city);
        tv_date = (TextView) findViewById(R.id.id_tv_date);
        tv_max_temp = (TextView) findViewById(R.id.id_tv_max_temp);
        tv_min_temp = (TextView) findViewById(R.id.id_tv_min_temp);
        tv_temp = (TextView) findViewById(R.id.id_tv_temp);
        tv_cond = (TextView) findViewById(R.id.id_tv_cond);
        tv_dir = (TextView) findViewById(R.id.id_tv_dir);
        tv_comf = (TextView) findViewById(R.id.id_tv_comf);
        tv_suggest = (TextView) findViewById(R.id.id_tv_suggest);

        title_pre = (TextView) findViewById(R.id.title_pre);
        title_pre_day1 = (TextView) findViewById(R.id.title_pre_day1);
        title_pre_day2 = (TextView) findViewById(R.id.title_pre_day2);
        title_pre_day3 = (TextView) findViewById(R.id.title_pre_day3);

                 //The weather in the next three days
        tv_pre_day1_maxtmp = (TextView) findViewById(R.id.tv_pre_day1_maxtmp);
        tv_pre_day1_mintmp = (TextView) findViewById(R.id.tv_pre_day1_mintmp);
        tv_pre_day1_cond = (TextView) findViewById(R.id.tv_pre_day1_cond);
        tv_pre_day1_wind = (TextView) findViewById(R.id.tv_pre_day1_wind);

        tv_pre_day2_maxtmp = (TextView) findViewById(R.id.tv_pre_day2_maxtmp);
        tv_pre_day2_mintmp = (TextView) findViewById(R.id.tv_pre_day2_mintmp);
        tv_pre_day2_cond = (TextView) findViewById(R.id.tv_pre_day2_cond);
        tv_pre_day2_wind = (TextView) findViewById(R.id.tv_pre_day2_wind);

        tv_pre_day3_maxtmp = (TextView) findViewById(R.id.tv_pre_day3_maxtmp);
        tv_pre_day3_mintmp = (TextView) findViewById(R.id.tv_pre_day3_mintmp);
        tv_pre_day3_cond = (TextView) findViewById(R.id.tv_pre_day3_cond);
        tv_pre_day3_wind = (TextView) findViewById(R.id.tv_pre_day3_wind);

    }

    public void weatherClick(View view) {
        location = et_location.getText();
        String url = "https://free-api.heweather.com/s6/weather?location=" + location + "&key=0754508fbe0f44be915c65a0182b8aa9";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ly_container.setBackgroundResource(R.mipmap.sky);
                title_pre.setVisibility(View.VISIBLE);
                title_pre_day1.setVisibility(View.VISIBLE);
                title_pre_day2.setVisibility(View.VISIBLE);
                title_pre_day3.setVisibility(View.VISIBLE);

                System.out.println(jsonObject);
                Gson gson = new Gson();
                WeatherBean weatherBean = gson.fromJson(jsonObject.toString(), WeatherBean.class);
                String cityName = weatherBean.getHeWeather6().get(0).getBasic().getLocation();
                String date = weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getDate();
                String maxtemp = weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_max();
                String mintemp = weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_min();
                String temp = weatherBean.getHeWeather6().get(0).getNow().getTmp();
                String cond = weatherBean.getHeWeather6().get(0).getNow().getCond_txt();
                String dir = weatherBean.getHeWeather6().get(0).getNow().getWind_dir();
                String comf = weatherBean.getHeWeather6().get(0).getLifestyle().get(0).getBrf();
                String suggest = weatherBean.getHeWeather6().get(0).getLifestyle().get(0).getTxt();

                String day1_maxtmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_max();
                String day1_mintmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_min();
                String day1_cond=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getCond_txt_n();
                String day1_wind=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(0).getWind_dir();

                String day2_maxtmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(1).getTmp_max();
                String day2_mintmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(1).getTmp_min();
                String day2_cond=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(1).getCond_txt_n();
                String day2_wind=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(1).getWind_dir();

                String day3_maxtmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(2).getTmp_max();
                String day3_mintmp=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(2).getTmp_min();
                String day3_cond=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(2).getCond_txt_n();
                String day3_wind=weatherBean.getHeWeather6().get(0).getDaily_forecast().get(2).getWind_dir();

                                 tv_city.setText("City:" + cityName);
                                 tv_date.setText("Date:" + date);
                                 tv_max_temp.setText("Maximum temperature:" + maxtemp);
                                 tv_min_temp.setText("Minimum temperature:" + mintemp);
                                 tv_temp.setText("Real-time temperature:" + temp);
                                 tv_cond.setText("Weather conditions:" + cond);
                                 tv_dir.setText("Wind direction:" + dir);
                                 tv_comf.setText("Human sensation:" + comf);
                                 tv_suggest.setText("Suggest:" + suggest);

                                 tv_pre_day1_maxtmp.setText("Maximum temperature:" + day1_maxtmp);
                                 tv_pre_day1_mintmp.setText("Minimum temperature:" + day1_mintmp);
                                 tv_pre_day1_cond.setText("Weather:" + day1_cond);
                                 tv_pre_day1_wind.setText("Wind direction:" + day1_wind);

                                 tv_pre_day2_maxtmp.setText("Maximum temperature:" + day2_maxtmp);
                                 tv_pre_day2_mintmp.setText("Minimum temperature:" + day2_mintmp);
                                 tv_pre_day2_cond.setText("Weather:" + day2_cond);
                                 tv_pre_day2_wind.setText("Wind direction:" + day2_wind);

                                 tv_pre_day3_maxtmp.setText("Maximum temperature:" + day3_maxtmp);
                                 tv_pre_day3_mintmp.setText("Minimum temperature:" + day3_mintmp);
                                 tv_pre_day3_cond.setText("Weather:" + day3_cond);
                                 tv_pre_day3_wind.setText("Wind direction:" + day3_wind);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println(volleyError);
            }
        });
        queue.add(request);
    }
}

Because I used a lot of TextView when writing this code, so as not to mess up, here is my xml code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@mipmap/girl"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:id="@+id/ly_container">

    <TextView
        android:id="@+id/id_tv_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_max_temp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_min_temp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_temp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_cond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_dir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_comf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/id_tv_suggest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/title_pre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Weather conditions in the next three days"
        android:paddingTop="20dp"
        android:visibility="gone"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp" />

    <LinearLayout
        android:paddingTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title_pre_day1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                                 android:text="Tomorrow"
                android:visibility="gone"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day1_maxtmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day1_mintmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day1_cond"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day1_wind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title_pre_day2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                                 android:text="Acquired"
                android:visibility="gone"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day2_maxtmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day2_mintmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day2_cond"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day2_wind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title_pre_day3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                                 android:text="The day after tomorrow"
                android:visibility="gone"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day3_maxtmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day3_mintmp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day3_cond"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_pre_day3_wind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom|center_horizontal"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_location"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
                         android:hint="Please enter the city" />

        <Button
            android:id="@+id/button_click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="weatherClick"
            android:background="@android:color/holo_green_light"
                         android:text="Query the weather" />
    </LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>

By the way, remember to give AndroidManifest a permission to access the network, otherwise how do we request data?

Okay, a lot of nonsense, just go to the final renderings. . . . . .

Just change the background image to whatever you like, haha, this is not the point.

Click the "Query Weather" button to switch the background image and get the JSON data and parse it out. Well, at this end, the blogger slipped...


Intelligent Recommendation

Free weather API, national weather JSON API interface, you can get a five-day weather forecast

Preface Stable and efficient weather API interface is rare. This station developed a perpetual calendar some time ago. It takes a few days of weather forecasting, integration and fault tolerance to de...

Free weather API, national weather JSON API interface, you can get five-day weather forecast

Stable and efficientWeather API interfaceIt is very rare. Some time ago, this site developed a perpetual calendar, which requires a weather forecast. It took a few days to integrate and fault-tolerate...

Implementation of the weather forecast app on the Android side (two) obtaining the Aliyun weather forecast API

The last chapter basically achieved the sliding effect of the control, so how do you get the data? I am using Alibaba Cloud weather forecast api. To use the API provided by Alibaba Cloud, you need to ...

Mobile App Development Practice - Task14 - Configuring a simple weather forecast interface

Mobile App Development Practice - Task14 - Configuring a simple weather forecast interface purpose 1. Creating the main Layui 2.WeatherActivity purpose Design a suggested weather forecast UI, as shown...

Request the weather API online and parse the json data to display it

Android network and data storage Chapter II Learning Request the weather API online and parse the json data to display it summary: Request the Internet information provider and get the returned data t...

More Recommendation

Weather forecast app based on Android development (source download)

Weather app developed based on AndroidStudio environment -Overall system introduction: This weather app uses AndroidStudio, an IDE tool, to be developed under Windows10. Mainly realized: 1. Positionin...

Android development weather forecast (3) using Baidu API to locate the city

Use Baidu API to locate cities Baidu Map API Introduction Get the key Import library file Configuring AndroidManifest.xml Write your own locator Baidu Map API Introduction Get the key (1) Login to Bai...

Android use interface to obtain weather and wind data show weather Weather in the APP

 The company APP project needs to be able to be able to display the current weather, find a lot of weather data interface, conclude either charge or unfriendly with them, and finally with the rec...

Crawling weather forecast data with API

Find the lowest temperature in the next three days below 5 degrees $lt, $lte, $gt, $gte for < , <=, >, >=...

Beautiful weather forecast, weather forecast API, 7-day accurate weather forecast API JSON

In order to improve the weather forecast function module, I developed a set of weather forecasts with relatively complete data volume, mainly for major cities across the country. The address is:http:/...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top