刚入坑安卓不久,写了个小东西试试手。

   工程文件我也找不见了,抱歉各位

    在此感谢https://blog.csdn.net/champion0324/article/details/71304567的分享。大的框架就是抄的。(⊙o⊙)…

    API这里我并没有用和风天气的,貌似不免费开放了。感谢室友找的api。。。

  http://api.map.baidu.com/telematics/v3/weather?location=长沙&output=json&ak=vZlRYC39tTuniYzNcX2zrQmZzblZcXwp

返回的是json数据,解析一下就可以得到数据。

volley框架也不好找,这里直接贴上我zhoa

链接: https://pan.baidu.com/s/1cIUpMY7RJIneUpXZi-4xPg 提取码: d1va

例如

{    "error": 0,    "status": "success",    "date": "2018-10-08",    "results": [        {            "currentCity": "长沙",            "pm25": "132",            "index": [                {                    "des": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",                    "tipt": "穿衣指数",                    "title": "穿衣",                    "zs": "舒适"                },                {                    "des": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。",                    "tipt": "洗车指数",                    "title": "洗车",                    "zs": "不宜"                },                {                    "des": "天气转凉,空气湿度较大,较易发生感冒,体质较弱的朋友请注意适当防护。",                    "tipt": "感冒指数",                    "title": "感冒",                    "zs": "较易发"                },                {                    "des": "天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。",                    "tipt": "运动指数",                    "title": "运动",                    "zs": "适宜"                },                {                    "des": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。",                    "tipt": "紫外线强度指数",                    "title": "紫外线强度",                    "zs": "弱"                }            ],            "weather_data": [                {                    "date": "周一 10月08日 (实时:21℃)",                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",                    "weather": "多云转小雨",                    "wind": "北风微风",                    "temperature": "26 ~ 18℃"                },                {                    "date": "周二",                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",                    "weather": "小雨",                    "wind": "北风微风",                    "temperature": "20 ~ 17℃"                },                {                    "date": "周三",                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png",                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/yin.png",                    "weather": "阴",                    "wind": "北风微风",                    "temperature": "24 ~ 15℃"                },                {                    "date": "周四",                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",                    "weather": "多云",                    "wind": "北风微风",                    "temperature": "23 ~ 13℃"                }            ]        }    ]}

在此,推荐一个解析的软件,postman,可以让解析更加清晰。

先看看之前的界面。low的一比。。。于是后面优化了一下。

 框架大体是一样的。记得在AndroidManifest中添加权限:在标签页内添加

  

mainactivity代码见下,主要就是调用解析,并将部分数据用intent传入到第二界面。其中解析之后使用的list,可能有点费劲。

package com.example.a_new_start;import android.app.Activity;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.Editable;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.google.gson.Gson;import org.json.JSONObject;import java.util.List;public class MainActivity extends Activity implements View.OnClickListener {    int flag_right = 0;    RequestQueue queue = null;    EditText et_city;    TextView tv_city, tv_nowtemp, tv_pm25, tv_pm_degree;    TextView tv_11, tv_12, tv_13, tv_21, tv_22, tv_23, tv_31, tv_32, tv_33, tv_41, tv_42, tv_43;    Button jump;    Editable city;    String des_1, des_2, des_3, des_4, des_5, tipt_zs_1, tipt_zs_2, tipt_zs_3, tipt_zs_4, tipt_zs_5;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        queue = Volley.newRequestQueue(this);        et_city = (EditText) findViewById(R.id.et_city);        tv_city = (TextView) findViewById(R.id.id_tv_city);        tv_pm25 = (TextView) findViewById(R.id.tv_pm25);        tv_pm_degree = (TextView) findViewById(R.id.tv_pm_degree);        tv_nowtemp = (TextView) findViewById(R.id.id_tv_nowtemp);        jump = (Button) findViewById(R.id.btn_jump);        jump.setOnClickListener(this);        tv_11 = (TextView) findViewById(R.id.tv_11);        tv_12 = (TextView) findViewById(R.id.tv_12);        tv_13 = (TextView) findViewById(R.id.tv_13);        tv_21 = (TextView) findViewById(R.id.tv_21);        tv_22 = (TextView) findViewById(R.id.tv_22);        tv_23 = (TextView) findViewById(R.id.tv_23);        tv_31 = (TextView) findViewById(R.id.tv_31);        tv_32 = (TextView) findViewById(R.id.tv_32);        tv_33 = (TextView) findViewById(R.id.tv_33);        tv_41 = (TextView) findViewById(R.id.tv_41);        tv_42 = (TextView) findViewById(R.id.tv_42);        tv_43 = (TextView) findViewById(R.id.tv_43);    }    public void weatherClick(View view) {        city = et_city.getText();        flag_right = 1;        String url = "http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=vZlRYC39tTuniYzNcX2zrQmZzblZcXwp";        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {            @Override            public void onResponse(JSONObject jsonObject) {                System.out.println(jsonObject);                Gson gson = new Gson();                WeatherBean jiepanxia = gson.fromJson(jsonObject.toString(), WeatherBean.class);                if (jiepanxia.getError() == 0) {                    //  List jiepanxia;                    String locate = jiepanxia.getResults().get(0).getCurrentCity();   //城市                    String date = jiepanxia.getResults().get(0).getWeather_data().get(0).getDate();  //日期                    String s_pm = jiepanxia.getResults().get(0).getPm25();                    String empty = "";    //一个空的字符串,坑爹api县级市没有pm2.5指数                    StringBuffer pm_degree = new StringBuffer();                    int flag = 0;                    if (s_pm.equals(empty) == false) {                        flag = 1;                        int pmint = Integer.parseInt(s_pm);                        if (pmint < 50)             //0-50                            pm_degree.append("空气质量:优");                        else if (pmint < 100)      //50-100                            pm_degree.append("空气质量:良");                        else if (pmint < 150)                            pm_degree.append("空气质量:轻度污染");                        else if (pmint < 200)                            pm_degree.append("空气质量:中度污染");                        else if (pmint < 300)                            pm_degree.append("空气质量:重度污染");                        else                            pm_degree.append("空气质量:严重污染");                    }                    String tv11 = jiepanxia.getResults().get(0).getWeather_data().get(0).getDate();             //相当于一个行列矩阵。具体看实际图片                    String tv12 = jiepanxia.getResults().get(0).getWeather_data().get(0).getWeather();                    String tv13 = jiepanxia.getResults().get(0).getWeather_data().get(0).getTemperature();                    String tv21 = jiepanxia.getResults().get(0).getWeather_data().get(1).getDate();                    String tv22 = jiepanxia.getResults().get(0).getWeather_data().get(1).getWeather();                    String tv23 = jiepanxia.getResults().get(0).getWeather_data().get(1).getTemperature();                    String tv31 = jiepanxia.getResults().get(0).getWeather_data().get(2).getDate();                    String tv32 = jiepanxia.getResults().get(0).getWeather_data().get(2).getWeather();                    String tv33 = jiepanxia.getResults().get(0).getWeather_data().get(2).getTemperature();                    String tv41 = jiepanxia.getResults().get(0).getWeather_data().get(3).getDate();                    String tv42 = jiepanxia.getResults().get(0).getWeather_data().get(3).getWeather();                    String tv43 = jiepanxia.getResults().get(0).getWeather_data().get(3).getTemperature();                    tipt_zs_1 = jiepanxia.getResults().get(0).getIndex().get(0).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(0).getZs();                    tipt_zs_2 = jiepanxia.getResults().get(0).getIndex().get(1).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(1).getZs();                    tipt_zs_3 = jiepanxia.getResults().get(0).getIndex().get(2).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(2).getZs();                    tipt_zs_4 = jiepanxia.getResults().get(0).getIndex().get(3).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(3).getZs();                    tipt_zs_5 = jiepanxia.getResults().get(0).getIndex().get(4).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(4).getZs();                    des_1 = jiepanxia.getResults().get(0).getIndex().get(0).getDes();                    des_2 = jiepanxia.getResults().get(0).getIndex().get(1).getDes();                    des_3 = jiepanxia.getResults().get(0).getIndex().get(2).getDes();                    des_4 = jiepanxia.getResults().get(0).getIndex().get(3).getDes();                    des_5 = jiepanxia.getResults().get(0).getIndex().get(4).getDes();                    char[] data_1 = tv11.toCharArray();                    StringBuffer the = new StringBuffer();                    the.append(data_1[0]);                    the.append(data_1[1]);                    the.append("(本日)");                                        char[] datearr = date.toCharArray();                    StringBuffer now = new StringBuffer();                    int k = 0;                    for (int i = 0; i < date.length(); i++) {                        if (datearr[i] == ':') {                            k = i;                            break;                        }                    }                    for (int i = k + 1; datearr[i] != ')'; i++) {                        now.append(datearr[i]);                    }                    tv_11.setText(the.toString());                    tv_12.setText(tv12);                    tv_13.setText(tv13);                    tv_21.setText(tv21);                    tv_22.setText(tv22);                    tv_23.setText(tv23);                    tv_31.setText(tv31);                    tv_32.setText(tv32);                    tv_33.setText(tv33);                    tv_41.setText(tv41);                    tv_42.setText(tv42);                    tv_43.setText(tv43);                    tv_city.setText(locate);                    tv_nowtemp.setText(now.toString());                    if (flag == 1)                        tv_pm25.setText("pm2.5值:" + s_pm);                    tv_pm_degree.setText(pm_degree.toString());                } else {                    Toast.makeText(getApplicationContext(), "城市名称错误,请重试", Toast.LENGTH_SHORT).show();                }            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                System.out.println(volleyError);            }        });        queue.add(request);    }    @Override    public void onClick(View v) {        if (v.getId() == R.id.btn_jump) {            if (flag_right == 1) {     //非空                Intent intent = new Intent(this, second.class);                intent.putExtra("t1", tipt_zs_1);                intent.putExtra("t2", tipt_zs_2);                intent.putExtra("t3", tipt_zs_3);                intent.putExtra("t4", tipt_zs_4);                intent.putExtra("t5", tipt_zs_5);                intent.putExtra("d1", des_1);                intent.putExtra("d2", des_2);                intent.putExtra("d3", des_3);                intent.putExtra("d4", des_4);                intent.putExtra("d5", des_5);                startActivity(intent);            } else {                Toast.makeText(getApplicationContext(), "请先查询城市天气", Toast.LENGTH_SHORT).show();            }        }    }}

主界面代码。

<?xml version="1.0" encoding="utf-8"?>                    

附上实际图。

视图

第二界面java代码

package com.example.a_new_start;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class second extends Activity {    TextView t_1,t_2,t_3,t_4,t_5,d_1,d_2,d_3,d_4,d_5;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.line_second);        Intent intent =getIntent();        String t1=intent.getStringExtra("t1");        String t2=intent.getStringExtra("t2");        String t3=intent.getStringExtra("t3");        String t4=intent.getStringExtra("t4");        String t5=intent.getStringExtra("t5");        String d1=intent.getStringExtra("d1");        String d2=intent.getStringExtra("d2");        String d3=intent.getStringExtra("d3");        String d4=intent.getStringExtra("d4");        String d5=intent.getStringExtra("d5");        t_1=(TextView)findViewById(R.id.tv_tipt_zs_1);        t_2=(TextView)findViewById(R.id.tv_tipt_zs_2);        t_3=(TextView)findViewById(R.id.tv_tipt_zs_3);        t_4=(TextView)findViewById(R.id.tv_tipt_zs_4);        t_5=(TextView)findViewById(R.id.tv_tipt_zs_5);        d_1=(TextView)findViewById(R.id.tv_des_1);        d_2=(TextView)findViewById(R.id.tv_des_2);        d_3=(TextView)findViewById(R.id.tv_des_3);        d_4=(TextView)findViewById(R.id.tv_des_4);        d_5=(TextView)findViewById(R.id.tv_des_5);                t_1.setText(t1);        t_2.setText(t2);        t_3.setText(t3);        t_4.setText(t4);        t_5.setText(t5);        d_1.setText(d1);        d_2.setText(d2);        d_3.setText(d3);        d_4.setText(d4);        d_5.setText(d5);    }}

最后是xml代码

<?xml version="1.0" encoding="utf-8"?>                                                                                                                                                                

还有自动生成的json代码

package com.example.a_new_start;import java.util.List;public class WeatherBean{    /**     * error : 0     * status : success     * date : 2018-10-02     * results : [{"currentCity":"changsha","pm25":"55","index":[{"des":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","tipt":"穿衣指数","title":"穿衣","zs":"热"},{"des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。","tipt":"洗车指数","title":"洗车","zs":"较适宜"},{"des":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","tipt":"感冒指数","title":"感冒","zs":"少发"},{"des":"天气较好,户外运动请注意防晒,推荐您在室内进行低强度运动。","tipt":"运动指数","title":"运动","zs":"较适宜"},{"des":"紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。","tipt":"紫外线强度指数","title":"紫外线强度","zs":"强"}],"weather_data":[{"date":"周二 10月02日 (实时:24℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"多云转晴","wind":"北风3-4级","temperature":"28 ~ 18℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"无持续风向微风","temperature":"29 ~ 18℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"无持续风向微风","temperature":"29 ~ 18℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"北风微风","temperature":"28 ~ 18℃"}]}]     */    private int error;    private String status;    private String date;    private List results;    public int getError() {        return error;    }    public void setError(int error) {        this.error = error;    }    public String getStatus() {        return status;    }    public void setStatus(String status) {        this.status = status;    }    public String getDate() {        return date;    }    public void setDate(String date) {        this.date = date;    }    public List getResults() {        return results;    }    public void setResults(List results) {        this.results = results;    }    public static class ResultsBean {        /**         * currentCity : changsha         * pm25 : 55         * index : [{"des":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","tipt":"穿衣指数","title":"穿衣","zs":"热"},{"des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。","tipt":"洗车指数","title":"洗车","zs":"较适宜"},{"des":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","tipt":"感冒指数","title":"感冒","zs":"少发"},{"des":"天气较好,户外运动请注意防晒,推荐您在室内进行低强度运动。","tipt":"运动指数","title":"运动","zs":"较适宜"},{"des":"紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。","tipt":"紫外线强度指数","title":"紫外线强度","zs":"强"}]         * weather_data : [{"date":"周二 10月02日 (实时:24℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"多云转晴","wind":"北风3-4级","temperature":"28 ~ 18℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"无持续风向微风","temperature":"29 ~ 18℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"无持续风向微风","temperature":"29 ~ 18℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"北风微风","temperature":"28 ~ 18℃"}]         */        private String currentCity;        private String pm25;        private List index;        private List weather_data;        public String getCurrentCity() {            return currentCity;        }        public void setCurrentCity(String currentCity) {            this.currentCity = currentCity;        }        public String getPm25() {            return pm25;        }        public void setPm25(String pm25) {            this.pm25 = pm25;        }        public List getIndex() {            return index;        }        public void setIndex(List index) {            this.index = index;        }        public List getWeather_data() {            return weather_data;        }        public void setWeather_data(List weather_data) {            this.weather_data = weather_data;        }        public static class IndexBean {            /**             * des : 天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。             * tipt : 穿衣指数             * title : 穿衣             * zs : 热             */            private String des;            private String tipt;            private String title;            private String zs;            public String getDes() {                return des;            }            public void setDes(String des) {                this.des = des;            }            public String getTipt() {                return tipt;            }            public void setTipt(String tipt) {                this.tipt = tipt;            }            public String getTitle() {                return title;            }            public void setTitle(String title) {                this.title = title;            }            public String getZs() {                return zs;            }            public void setZs(String zs) {                this.zs = zs;            }        }        public static class WeatherDataBean {            /**             * date : 周二 10月02日 (实时:24℃)             * dayPictureUrl : http://api.map.baidu.com/images/weather/day/duoyun.png             * nightPictureUrl : http://api.map.baidu.com/images/weather/night/qing.png             * weather : 多云转晴             * wind : 北风3-4级             * temperature : 28 ~ 18℃             */            private String date;            private String dayPictureUrl;            private String nightPictureUrl;            private String weather;            private String wind;            private String temperature;            public String getDate() {                return date;            }            public void setDate(String date) {                this.date = date;            }            public String getDayPictureUrl() {                return dayPictureUrl;            }            public void setDayPictureUrl(String dayPictureUrl) {                this.dayPictureUrl = dayPictureUrl;            }            public String getNightPictureUrl() {                return nightPictureUrl;            }            public void setNightPictureUrl(String nightPictureUrl) {                this.nightPictureUrl = nightPictureUrl;            }            public String getWeather() {                return weather;            }            public void setWeather(String weather) {                this.weather = weather;            }            public String getWind() {                return wind;            }            public void setWind(String wind) {                this.wind = wind;            }            public String getTemperature() {                return temperature;            }            public void setTemperature(String temperature) {                this.temperature = temperature;            }        }    }}

有任何问题,欢迎留言~

更多相关文章

  1. 十款 Material Design 风格的 Android(安卓)开源项目
  2. Android开发--身高体重指数(BIM)计算--访问标识符号(android:id属性/
  3. Android(安卓)RSA 公钥加密 遇到坑
  4. Android新浪星座运势程序开发
  5. Android使用wheelView实现简单类似ios PickerView选择器效果
  6. 程序员的双十一剁手指南,看完不剁手算我输。
  7. 流程图控件GoJS内置GraphObject类各指数介绍(二)
  8. Android(安卓)SAX解析XML
  9. 调用对象[置顶] Android通过调用Webservice实现天气预报

随机推荐

  1. android导入源码编译的一个错误
  2. Android之android.os.DeadObjectExceptio
  3. Android中的Notification
  4. 开发遇到的low坑
  5. 安卓课程二十三 ImageView实现适屏和裁剪
  6. Android 技术目标:全栈工程师
  7. android更改暗码
  8. Android保存Serializable数据到本地
  9. Android原生下载管理相关记录
  10. Android > 浅谈获取时间