简约便捷的音天气预报app

自己最近慢慢摸索写了一个简约便捷的天气预报app,先发上来供大家参考和自己复习学习。## api ##首先我们需要一个api,我采用的是百度天气的api,具体步骤大家可以参考http://lbsyun.baidu.com/index.php?title=car/api/weather

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/bj2"xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout
        android:layout_width="wrap_content"android:layout_height="80dp"android:orientation="horizontal"><ImageView
            android:id="@+id/imgtop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/duoyun"android:layout_marginLeft="10dp"android:layout_marginTop="20dp"/><TextView
            android:id="@+id/wendu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="50dp"android:layout_marginTop="20dp"android:textColor="@color/colorAccent"/><EditText
            android:id="@+id/etcityname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:textColor="#ffffff"android:hint="输入城市名称"android:textColorHint="#ffffff"android:textSize="24dp"/><Button
            android:id="@+id/sousuo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:textColor="#ffffff"android:text="搜索"/></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"android:gravity="center"><TextView
            android:id="@+id/date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"/></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"><TextView
            android:id="@+id/city"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="80dp"android:textColor="#ffffff"/></LinearLayout><TextView
        android:layout_width="match_parent"android:layout_height="1dp"android:background="#ffffff"/>
<ScrollView
    android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayout
       android:layout_width="match_parent"android:layout_height="100dp"android:layout_marginTop="5dp"android:orientation="horizontal"><LinearLayout
           android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:orientation="vertical"><TextView
               android:layout_width="match_parent"android:layout_height="30dp"android:text="wind"android:gravity="center"android:textColor="#ffffff"android:textSize="20dp"/><TextView
               android:id="@+id/wind"android:layout_width="match_parent"android:layout_height="60dp"android:textSize="20dp"android:textColor="#ffffff"android:gravity="center"/></LinearLayout><TextView
           android:layout_width="1dp"android:layout_height="match_parent"android:background="#55000000" /><LinearLayout
           android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:orientation="vertical"><TextView
               android:layout_width="match_parent"android:layout_height="30dp"android:text="weather"android:gravity="center"android:textColor="#ffffff"android:textSize="20dp"/><TextView
               android:id="@+id/temperature"android:layout_width="match_parent"android:layout_height="60dp"android:textSize="20dp"android:textColor="#ffffff"android:gravity="center"/></LinearLayout><TextView
           android:layout_width="1dp"android:layout_height="match_parent"android:background="#55000000" /><LinearLayout
           android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:orientation="vertical"><TextView
               android:layout_width="match_parent"android:layout_height="30dp"android:text="PM2.5"android:gravity="center"android:textColor="#ffffff"android:textSize="20dp"/><TextView
               android:id="@+id/pm25"android:layout_width="match_parent"android:layout_height="60dp"android:textSize="20dp"android:textColor="#ffffff"android:gravity="center"/></LinearLayout><TextView
           android:layout_width="1dp"android:layout_height="match_parent"android:background="#55000000" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextView
            android:layout_width="match_parent"android:layout_height="1dp"android:background="#ffffff"/><TextView
            android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="指数"android:textSize="40dp"android:textColor="@color/colorAccent"/><TextView
            android:layout_width="match_parent"android:layout_height="1dp"android:background="#ffffff"/><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView
                android:layout_width="100dp"android:layout_height="wrap_content"android:textColor="#ffffff"android:gravity="center"android:textSize="24dp"android:text="穿衣"/><TextView
                android:id="@+id/chuanyi"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"android:layout_marginLeft="10dp"android:text="适合穿衣"/></LinearLayout><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView
                android:layout_width="100dp"android:layout_height="wrap_content"android:textColor="#ffffff"android:gravity="center"android:textSize="24dp"android:text="洗车"/><TextView
                android:id="@+id/xiche"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"android:layout_marginLeft="10dp"android:text="适合穿衣"/></LinearLayout><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView
                android:layout_width="100dp"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="24dp"android:gravity="center"android:text="运动"/><TextView
                android:id="@+id/ganmao"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"android:layout_marginLeft="10dp"android:text="适合穿衣"/></LinearLayout><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView
                android:layout_width="100dp"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="24dp"android:gravity="center"android:text="感冒"/><TextView
                android:id="@+id/yundong"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"android:layout_marginLeft="10dp"android:text="适合穿衣"/></LinearLayout><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView
                android:layout_width="100dp"android:layout_height="wrap_content"android:textColor="#ffffff"android:gravity="center"android:textSize="24dp"android:text="紫外线"/><TextView
                android:id="@+id/ziwaixian"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"android:layout_marginLeft="10dp"android:text="适合穿衣"/></LinearLayout></LinearLayout><TextView
        android:layout_width="match_parent"android:layout_height="1dp"android:background="#ffffff"/><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayout
            android:layout_weight="1"android:orientation="vertical"android:layout_width="0dp"android:layout_height="wrap_content"><TextView
                android:layout_gravity="center"android:id="@+id/diertian"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:textColor="#ffffff"/><ImageView
                android:layout_gravity="center"android:id="@+id/imdiertian"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/duoyun"/><TextView
                android:layout_gravity="center"android:id="@+id/wdiertian"android:layout_width="wrap_content"android:layout_height="45dp"android:textColor="#ffffff"android:textSize="20dp"/><TextView
                android:layout_gravity="center"android:id="@+id/tdiertian"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"/></LinearLayout><LinearLayout
            android:layout_weight="1"android:orientation="vertical"android:layout_width="0dp"android:layout_height="wrap_content"><TextView
                android:id="@+id/disantian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:textColor="#ffffff"/><ImageView
                android:id="@+id/imdisantian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/duoyun"/><TextView
                android:layout_gravity="center"android:id="@+id/wdisantian"android:layout_width="wrap_content"android:layout_height="45dp"android:textColor="#ffffff"android:textSize="20dp"/><TextView
                android:id="@+id/tdisantian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"/></LinearLayout><LinearLayout
            android:layout_weight="1"android:orientation="vertical"android:layout_width="0dp"android:layout_height="wrap_content"><TextView
                android:id="@+id/disitian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:textColor="#ffffff"/><ImageView
                android:id="@+id/imdisitian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/duoyun"/><TextView
                android:layout_gravity="center"android:id="@+id/wdisitian"android:layout_width="wrap_content"android:layout_height="45dp"android:textColor="#ffffff"android:textSize="20dp"/><TextView
                android:id="@+id/tdisitian"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="20dp"/></LinearLayout></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

主要是LinearLayout和一个ScrollView的嵌套很简单就不多说了。

网络请求

/20:40:90:AE:E8:19:FD:95:21:F2:CD:D5:72:D3:EE:CB:38:F9:B7:9C这是发布版SHA1,com.example.administrator.zhuanjia这是包名/
private void sendRequestWithHttpURLConnection() { // 开启线程来发起网络请求
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
StringBuilder cityname=new StringBuilder();
try {
cityname.append(“http://api.map.baidu.com/telematics/v3/weather?location=“).
append(URLEncoder.encode(etcityname.getText().toString(),”UTF-8”)).
append(“&mcode=20:40:90:AE:E8:19:FD:95:21:F2:CD:D5:72:D3:EE:CB:38:F9:B7:9C;com.example.administrator.zhuanjia&output=json&ak=P0SE4gI053GYaVRONbWwR8Bz13a5OwiC”);
URL url = new URL(cityname.toString());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“GET”);
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream(); // 下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
// 将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
//if (connection != null) {
// connection.disconnect();
//}
}
}
}).start();
}

对返回的JSON进行解析

 public List<Map<String, Object>> getInformation(String jonString)throws Exception {JSONObject dataOfJson =new JSONObject(jonString);JSONArray results=dataOfJson.getJSONArray("results");JSONObject results0=results.getJSONObject(0);JSONArray index = results0.getJSONArray("index");JSONObject index0 = index.getJSONObject(0);//穿衣JSONObject index1 = index.getJSONObject(1);//洗车JSONObject index2 = index.getJSONObject(2);//感冒JSONObject index3 = index.getJSONObject(3);//运动JSONObject index4 = index.getJSONObject(4);//紫外线强度JSONArray weather_data = results0.getJSONArray("weather_data");//weather_data中有四项JSONObject weather_data0 = weather_data.getJSONObject(0);//今天JSONObject weather_data1 = weather_data.getJSONObject(1);//明天JSONObject weather_data2 = weather_data.getJSONObject(2);//后天JSONObject weather_data3 = weather_data.getJSONObject(3);//大后天List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("location",results0.getString("currentCity"));map.put("pm25",results0.getString("pm25"));map.put("chuanyi",index0.getString("des"));map.put("xiche",index1.getString("des"));map.put("ganmao",index2.getString("des"));map.put("yundong",index3.getString("des"));map.put("ziwaixian",index4.getString("des"));map.put("date",weather_data0.getString("date"));map.put("weather",weather_data0.getString("weather"));map.put("wind",weather_data0.getString("wind"));map.put("temperature",weather_data0.getString("temperature"));map.put("date1",weather_data1.getString("date"));map.put("weather1",weather_data1.getString("weather"));map.put("temperature1",weather_data1.getString("temperature"));map.put("date2",weather_data2.getString("date"));map.put("weather2",weather_data2.getString("weather"));map.put("temperature2",weather_data2.getString("temperature"));map.put("date3",weather_data3.getString("date"));map.put("weather3",weather_data3.getString("weather"));map.put("temperature3",weather_data3.getString("temperature"));all.add(map);return all;}

将获取的数据更新在UI上

 private Handler handler=new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case SHOW_RESPONSE:String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上Util util = new Util();try {List<Map<String, Object>> all = util.getInformation(response);Iterator<Map<String, Object>> iterator = all.iterator();while (iterator.hasNext()) {Map<String, Object> map = iterator.next();//Log.d("天气", map.get("weather").toString());city.setText(map.get("location").toString());backgrand(imgtop,map.get("weather").toString());pm25.setText(map.get("pm25").toString());chuanyi.setText(map.get("chuanyi").toString());xiche.setText(map.get("xiche").toString());ganmao.setText(map.get("ganmao").toString());yundong.setText(map.get("yundong").toString());ziwaixian.setText(map.get("ziwaixian").toString());wind.setText(map.get("wind").toString());temperature.setText(map.get("temperature").toString());date.setText(map.get("date").toString());diertian.setText(map.get("date1").toString());tdiertian.setText(map.get("temperature1").toString());disantian.setText(map.get("date2").toString());tdisantian.setText(map.get("temperature2").toString());disitian.setText(map.get("date3").toString());tdisitian.setText(map.get("temperature3").toString());wdiertian.setText(map.get("weather1").toString());wdisantian.setText(map.get("weather2").toString());wdisitian.setText(map.get("weather3").toString());backgrand(imdiertian,map.get("weather1").toString());backgrand(imdisantian,map.get("weather2").toString());backgrand(imdisitian,map.get("weather3").toString());}} catch (Exception e) {e.printStackTrace();}} }};

上面就是我的主要代码,本人菜鸟一枚如有错误或者不妥请大家指正,我的源码在我的github上欢迎大家关注我https://github.com/zkandroid/zhuanjia

简单的天气预报app相关推荐

  1. 一个简单的天气预报APP

    运行效果 一个简单的天气预报APP 项目代码 https://gitee.com/fakerxuan/weather

  2. Android上实现一个简单的天气预报APP(七) 切换到新的界面(选择城市界面)

    学习参考资源:https://www.gitbook.com/book/zhangqx/mini-weather/details 前面我们已经设置好了基本界面,并可以从网络上实时获取天气预报信息并解析 ...

  3. Android上实现一个简单的天气预报APP(六) 更新界面数据

    学习参考资源:https://www.gitbook.com/book/zhangqx/mini-weather/details 前面我们已经设置好了基本的界面,获取了网络上的天气数据并解析出来了,接 ...

  4. Android上实现一个简单的天气预报APP(十二) 未来三天的天气预报

    学习参考资源:https://www.gitbook.com/book/zhangqx/mini-weather/details 前面我们已经可以获取当天的天气数据,并在屏幕上更新数据了,接下来我们获 ...

  5. android天气预报项目总结报告,Android项目:天气预报App

    一 介绍 该项目是在Android Studio的环境下实现的,主要是仿照了小米10手机上的天气预报App. 二 效果图 三 页面介绍 1.主界面                            ...

  6. 用Swift实现一款天气预报APP(三)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 通过前面的学习,一个天气预报的APP已经基本可用了.至少 ...

  7. android入门学习-天气预报app(一)

    引言 学习<android第一行代码>根据书本开发的天气预报app,主要用于熟练操作android开发(android studio3.0平台). 今天主要分享一下从服务器上获取天气信息, ...

  8. 天津天气预报软件测试,可以查询节气的天气预报app

    查看 生活服务|0KB 更新时间:2021-07-21 12:49:38 评分:9.4 概要:啦朴天气是一款查询天气的软件,软件的天气都是实时更新的,每天都会更新天气的,这里的天气数据非常的准确,如果 ...

  9. Android简单版天气预报,显示天气预报(第二步)

    Android简单版天气预报,显示天气预报(第二步) 接着上一步的:[https://blog.csdn.net/weixin_44889138/article/details/102748057] ...

最新文章

  1. DOM操作表格的各种属性[z]
  2. 进度条设置_朋友圈可以设置quot;仅一个月可见quot;了,什么时候出语音进度条呢?内附陈粒小姐姐的新歌哦~...
  3. 用来用去还是觉得SDCMS好用
  4. 【POJ - 3744】Scout YYF I(概率dp,矩阵快速幂优化dp)
  5. 【线性代数本质】3:矩阵和线性变换的本质
  6. 单片机软件模拟SPI接口—加深理解SPI总线协议
  7. repeater中分页aspnetpager是遇到的问题
  8. 温度补偿计算公式_管道布置设计原则、基本要求与补偿器的选择
  9. ubuntu jdk 安装 【转载】
  10. 稀缺-我们是如何陷入贫穷与忙碌的 读后感
  11. 《码出高效:Java 开发手册》“码” 出高效的同时编写出高质量的代“码”。
  12. 数据库 关于权限管理系统的三种设计方法
  13. 美团外卖与饿了么竞品分析
  14. Android面试你必须要知道的那些知识,已拿offer附真题解析
  15. kali攻击wifi、破解wifi详细教程(仅供学习使用)
  16. php enc28j60,uip0.9+ENC28J60 缓存的读写发送
  17. Oracle之TO_DATE用法详解
  18. Python的两种继承方法
  19. 动手学习深度学习(总结梳理)——9. Pytorch神经网络基础
  20. ATF54143的ADS模型

热门文章

  1. java 10进制 和16进制相互转换
  2. 【财务系统】报告如何转换下载为word?这个方法值得借鉴
  3. 《洛克菲勒留给儿子的38封信》选摘
  4. 新媒体广告投放,新媒体广告投放渠道。
  5. 576芯SMC三网合一光缆交接箱单面开门
  6. 所见即所得:memoQ独立预览先人一步
  7. 毕业设计 U-Net遥感图像语义分割(源码+论文)
  8. JAVA——实现古典问题之不死神兔问题
  9. apache ii评分和死亡率_APACHEⅡ评分与血乳酸清除率对危重症患者预后评估的比较...
  10. Copy-Paste is All U Need!拥挤目标检测你是如何解决的呢?