1、项目效果图:

2、主页面MainActivity代码如下:

MainActivity.java

package com.qianfeng.weather;

import android.content.Intent;

import android.graphics.drawable.AnimationDrawable;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private ImageView refreshIv;

private ImageView searchIv;

private TextView cityTv;

private TextView pmTv;

private TextView errorTv;

private TextView tempTv;

private TextView weatherTv;

private TextView windTv;

private TextView dateTv;

private View lineView;

private LinearLayout otherLl;

private List weekList;

private Handler handler;

private int code = 101100101;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initData();

getData(code);

setListener();

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 100) {

code = resultCode;

getData(code);

}

}

private void setListener() {

refreshIv.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

getData(code);

}

});

searchIv.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent intent = new Intent(MainActivity.this,

SearchActivity.class);

startActivityForResult(intent, 100);

}

});

}

/**

*

*/

private void initData() {

//        在Android中借助handler类实现从服务器获取数据后更新UI页面(Handler原理)

handler = new Handler() {

@Override

public void handleMessage(Message msg) {

if (msg.what == 200) {

errorTv.setText((String) msg.obj);

}

if (msg.what == 100) {

errorTv.setText("");

//                    停止转圈动画

refreshIv.setBackgroundResource(R.mipmap.refresh);

List list = (List) msg.obj;

cityTv.setText(list.get(0).getCity());

pmTv.setText(list.get(0).getPm());

tempTv.setText(list.get(0).getTempCurrent());

weatherTv.setText(list.get(0).getWeather() + " " + list.get(0).getTemp());

windTv.setText(list.get(0).getWindCurrent());

dateTv.setText(list.get(0).getDate_y() + " " + list.get(0).getWeek());

if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("优")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm1));

} else if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("良")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm2));

} else if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("轻度污染")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm3));

} else if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("中度")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm4));

} else if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("重度")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm5));

} else if (list.get(0).getPm().substring(list.get(0).getPm().lastIndexOf(" ") + 1).equals("严重")) {

lineView.setBackgroundColor(getResources().getColor(R.color.pm6));

}

//                    清空水平滚动条的孙子辈视图

otherLl.removeAllViews();

//                    将未来五天的天气信息动态设置到水平滚动条中的线性布局中的子视图中

for (int i = 1; i < list.size(); i++) {

View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);

TextView weekItemTv = (TextView) view.findViewById(R.id.tv_week_item);

TextView weatherItemTv = (TextView) view.findViewById(R.id.tv_weather_item);

TextView tempItemTv = (TextView) view.findViewById(R.id.tv_temp_item);

weekItemTv.setText(list.get(i).getWeek());

weatherItemTv.setText(list.get(i).getWeather());

tempItemTv.setText(list.get(i).getTemp());

//                        动态将item视图添加到otherLl中

otherLl.addView(view);

}

}

}

};

weekList = new ArrayList<>();

weekList.add("星期一");

weekList.add("星期二");

weekList.add("星期三");

weekList.add("星期四");

weekList.add("星期五");

weekList.add("星期六");

weekList.add("星期日");

}

/**

* 北京

*/

private void initView() {

refreshIv = (ImageView) findViewById(R.id.refresh_iv);

searchIv = (ImageView) findViewById(R.id.search_iv);

cityTv = (TextView) findViewById(R.id.city_tv);

pmTv = (TextView) findViewById(R.id.pm_tv);

errorTv = (TextView) findViewById(R.id.error_tv);

tempTv = (TextView) findViewById(R.id.temp_tv);

weatherTv = (TextView) findViewById(R.id.weather_tv);

windTv = (TextView) findViewById(R.id.wind_tv);

dateTv = (TextView) findViewById(R.id.date_tv);

lineView = findViewById(R.id.line_view);

otherLl = (LinearLayout) findViewById(R.id.other_ll);

}

/**

* 获取网络数据

*/

private void getData(final int code) {

if (!NetUtils.isActive(MainActivity.this)) {

errorTv.setText("请确认是否有网");

Toast.makeText(MainActivity.this, "亲,确认您是否有网!", Toast.LENGTH_LONG).show();

return;

}

refreshIv.setBackgroundResource(R.drawable.pb_bg);

//封装 继承  多态

AnimationDrawable animationDrawable = (AnimationDrawable) refreshIv.getBackground();

animationDrawable.start();

//        开一个子线程进行网络请求  获取服务器json数据(注意:Android主线程不能操作耗时代码块)

new Thread(new Runnable() {

@Override

public void run() {

String uri = "http://weather.123.duba.net/static/weather_info/" + code + ".html";

String result = NetUtils.doGet(uri);

//                list集合中存的今天+未来五天的天气信息

List list = parserJson(result);

Message message = handler.obtainMessage();

//                集合数据为空时让其检查网络问题,或请程序员核查代码(未彻底优化代码结构以及代码性能等)

if (list == null || list.size() == 0) {

message.what = 200;

message.obj = "请检查网络";

handler.sendMessage(message);

return;

}

message.what = 100;

message.obj = list;

handler.sendMessage(message);

//                注意:子线程不能更新UI主线程的页面

//                cityTv.setText(list.get(0).getCity());

}

}).start();

}

/**

* 解析json数据

*

* @param result

*/

private List parserJson(String result) {

List list = new ArrayList<>();

try {

JSONObject jsonObject = new JSONObject(result);

JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");

//            保存今天+未来五天的天气数据

for (int i = 1; i < 7; i++) {

WeatherBean weatherBean = new WeatherBean();

if (i == 1) {

weatherBean.setCity(weatherInfo.getString("city"));

weatherBean.setCityId(weatherInfo.getString("cityid"));

weatherBean.setDate_y(weatherInfo.getString("date_y"));

weatherBean.setPm("PM:" + weatherInfo.getString("pm") + " " + weatherInfo.getString("pm-level"));

weatherBean.setTempCurrent(weatherInfo.getString("temp") + "°");

weatherBean.setWindCurrent(weatherInfo.getString("wd") + " " + weatherInfo.getString("ws"));

}

weatherBean.setWeek(getWeek(i, weatherInfo.getString("week")));

weatherBean.setTemp(weatherInfo.getString("temp" + i));

weatherBean.setWeather(weatherInfo.getString("weather" + i));

//                将每天的天气信息保存到集合中

list.add(weatherBean);

}

return list;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* @param i

* @param week

* @return

*/

private String getWeek(int i, String week) {

int index = 0;

for (int j = 0; j < weekList.size(); j++) {

if (weekList.get(j).equals(week)) {

index = j;

break;

//                continue;

}

}

if (index + i < 8) {

index = index + i - 1;

} else {

index = index + i - 8;

}

return weekList.get(index);

}

}

NetUtils(网络工具类):

package com.qianfeng.weather;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;

public class NetUtils {

/**

* 监测是否有网

*/

public static boolean isActive(Context context) {

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manager.getActiveNetworkInfo();

if (info != null) {

return info.isConnected();

}

return false;

}

/**

* 根据传过来的接口地址,返回服务器吐出来的字符串

*/

public static String doGet(String uri) {

StringBuffer stringBuffer = new StringBuffer();

String result = null;

URLConnection connection = null;

InputStream inputStream = null;

try {

URL url = new URL(uri);

connection = url.openConnection();

connection.setConnectTimeout(10 * 1000);

inputStream = connection.getInputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

String line = bufferedReader.readLine();

while (line != null) {

stringBuffer.append(line);

line = bufferedReader.readLine();

}

result = stringBuffer.substring(stringBuffer.indexOf("(") + 1, stringBuffer.lastIndexOf(")"));

return result;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

}

WeatherBean.java

package com.qianfeng.weather;

public class WeatherBean {

private String city;

private String cityId;

private String week;

private String temp;

private String date_y;

private String wind;

private String weather;

private String pm;

private String tempCurrent;

private String windCurrent;

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getCityId() {

return cityId;

}

public void setCityId(String cityId) {

this.cityId = cityId;

}

public String getWeek() {

return week;

}

public void setWeek(String week) {

this.week = week;

}

public String getTemp() {

return temp;

}

public void setTemp(String temp) {

this.temp = temp;

}

public String getDate_y() {

return date_y;

}

public void setDate_y(String date_y) {

this.date_y = date_y;

}

public String getWind() {

return wind;

}

public void setWind(String wind) {

this.wind = wind;

}

public String getWeather() {

return weather;

}

public void setWeather(String weather) {

this.weather = weather;

}

public String getPm() {

return pm;

}

public void setPm(String pm) {

this.pm = pm;

}

public String getTempCurrent() {

return tempCurrent;

}

public void setTempCurrent(String tempCurrent) {

this.tempCurrent = tempCurrent;

}

public String getWindCurrent() {

return windCurrent;

}

public void setWindCurrent(String windCurrent) {

this.windCurrent = windCurrent;

}

}

3、主页面XML布局如下:

activity_main.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@mipmap/background">

android:id="@+id/refresh_iv"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_margin="12dp"

android:background="@mipmap/refresh" />

android:id="@+id/search_iv"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_alignParentRight="true"

android:layout_margin="12dp"

android:background="@mipmap/search" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="12dp"

android:gravity="center_horizontal"

android:orientation="vertical">

android:id="@+id/city_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFF"

android:textSize="28sp" />

android:id="@+id/pm_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFF"

android:textSize="20sp" />

android:id="@+id/line_view"

android:layout_width="match_parent"

android:layout_height="10dp"

android:background="#6BCD07" />

android:id="@+id/error_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#F00"

android:textSize="20sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="12dp">

android:id="@+id/temp_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFF"

android:textSize="40sp" />

android:id="@+id/weather_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/temp_tv"

android:layout_toRightOf="@+id/temp_tv"

android:textColor="#FFF" />

android:id="@+id/wind_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/temp_tv"

android:textColor="#FFF" />

android:id="@+id/date_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/wind_tv"

android:textColor="#FFF" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true">

android:id="@+id/other_ll"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#6666"

android:orientation="horizontal">

item.xml(用在Java代码动态加载后五天天气信息Item小布局):

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical"

android:paddingLeft="5dp"

android:paddingRight="5dp">

android:id="@+id/tv_week_item"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="12dp"

android:layout_marginBottom="12dp"

android:text="星期三"

android:textColor="#FFF"

android:textSize="18sp" />

android:id="@+id/tv_weather_item"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="多云"

android:textColor="#FFF"

android:textSize="18sp" />

android:id="@+id/tv_temp_item"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="12dp"

android:layout_marginBottom="12dp"

android:text="20°C~29°C"

android:textColor="#FFF"

android:textSize="16sp" />

4、搜索页面实现SearchActivity.java,主要包含XML解析,具体代码如下:

SearchActivity.java

package com.qianfeng.weather;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageView;

import java.io.IOException;

import java.io.InputStream;

import java.util.Map;

public class SearchActivity extends AppCompatActivity {

private EditText auto = null;

private Map map = null;

private ImageView iv_search = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_search);

initData();

initView();

}

private void initView() {

auto = (EditText) findViewById(R.id.autoTV);

iv_search = (ImageView) findViewById(R.id.iv_search);

iv_search.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String city = auto.getText().toString().trim();

String codeStr = map.get(city);

if (codeStr != null) {

int code = Integer.parseInt(codeStr);

SearchActivity.this.setResult(code);

finish();

} else {

auto.setText("中国没有这样的城市,请重新输入"

+ "或"

+ "输入内容不能为空");

}

}

});

}

private void initData() {

try {

InputStream is = getAssets().open("city_code.xml");

map = new XMLParser().getMap(is);

} catch (IOException e) {

e.printStackTrace();

}

}

}

XMLParser(XML解析工具类)

package com.qianfeng.weather;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;

import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.security.Key;

import java.util.HashMap;

import java.util.Map;

import static java.net.Proxy.Type.HTTP;

public class XMLParser {

/**

*xml解析  pull  sax 。。。

*/

public Map getMap(InputStream is) {

Map map = new HashMap<>();

try {

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

XmlPullParser parser = factory.newPullParser();

parser.setInput(new BufferedReader(new InputStreamReader(is)));

//            parser.setInput(is,null);

//            XmlPullParser.END_DOCUMENT

//            XmlPullParser.START_DOCUMENT

//            XmlPullParser.START_TAG

//            XmlPullParser.END_TAG

//            XmlPullParser.TEXT

int eventType = parser.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {

if (eventType == XmlPullParser.START_TAG) {

String name = parser.getName();

if ("key".equals(name)) {

String key = parser.nextText();

parser.next();

parser.next();

String value = parser.nextText();

map.put(key, value);

}

}

parser.next();

eventType = parser.getEventType();

}

} catch (Exception e) {

e.printStackTrace();

}

return map;

}

}

activity_search.xml(搜索页面布局)

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@mipmap/background"

android:padding="5dp"

android:orientation="vertical" >

android:id="@+id/iv_search"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_marginRight="17dp"

android:src="@mipmap/search" />

android:id="@+id/autoTV"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentEnd="true"

android:layout_alignParentRight="true"

android:layout_marginEnd="41dp"

android:layout_marginRight="41dp"

android:hint="请输入城市" />

5、AndroidManifest清单文件如下:

package="com.qianfeng.weather">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

7、加载网络不好转圈动画用的自定义帧动画pb_bg.xml,具体代码如下:

android:drawable="@mipmap/pb00"

android:duration="200"/>

android:drawable="@mipmap/pb01"

android:duration="200"/>

android:drawable="@mipmap/pb02"

android:duration="200"/>

android:drawable="@mipmap/pb03"

android:duration="200"/>

android:drawable="@mipmap/pb04"

android:duration="200"/>

android:drawable="@mipmap/pb05"

android:duration="200"/>

android:drawable="@mipmap/pb06"

android:duration="200"/>

android:drawable="@mipmap/pb07"

android:duration="200"/>

android:drawable="@mipmap/pb08"

android:duration="200"/>

android:drawable="@mipmap/pb09"

android:duration="200"/>

android:drawable="@mipmap/pb10"

android:duration="200"/>

android:drawable="@mipmap/pb11"

android:duration="200"/>

6、资源图片等具体代码可参考本人共享发送上传代码资源Weather.zip

android天气预报实训程序清单,Android天气预报项目相关推荐

  1. android实训报告日记,android目实训报告.doc

    android目实训报告 Android项目实训报告 学号 201033201 姓名 ***项目 名称 记事本项 目 介 绍 我的项目分为心情日志,备忘 录和日记,有记录和查询的功能项 目 分 析 我 ...

  2. Android扫雷实训小结,扫雷课程实训报告.doc

    扫雷课程实训报告.doc 信息工程学院课程实训报告1姓名学号实训名称所在班级实训时间实训地点实训教师实训企业实训成绩一.实训基本信息实训项目名称扫雷游戏实训环境ECLIPSEJDK二.实训项目简介(包 ...

  3. python最强实训程序(增删改查)机房收费管理系统-基于tkinter的图形化界面(附详细代码)

    python最强实训程序(增删改查)机房收费管理系统-基于tkinter的图形化界面(附详细代码) 最近学校实训,用两天时间做了一个python小程序*机房收费管理系统*,一款基于tkinter使用p ...

  4. android 实训的背景,Android实训项目作业.doc

    Android实训项目作业 2-1用整型数计算两个数的和2 2-7排列任意4个数的顺序,按从小到大顺序输出2 2.1编写显示下列图形的程序.2 3.1编写程序,当点击按钮命令后,页面标题及文本组件的文 ...

  5. 安博河北实训基地java+android

    安博河北实训基地java软件开发工程师课程       安博河北实训基地android软件开发工程师课程                     艾迪教育 Android工程师简介• Android工 ...

  6. Android水平仪实训报告,水准仪测量实训报告

    水准仪测量实训报告 水准仪是提供水平视线来测定高差的仪器,主要有自动安平水准仪.微倾式水准仪.数字水准仪. 一.实习目的: 1.联系水准仪的安置.整平.瞄准与读书和怎样测定地面两点间的高程; 2.掌握 ...

  7. android计算器实训报告总结,大学生计算机实训报告

    大学生计算机实训报告 实训是职业技能实际训练的简称,是指在学校控制状态下,按照人才培养规律与目标,对学生进行职业技术应用能力训练的教学过程.小编今天为大家带来大学生计算机实训报告,一起来学习一下吧! ...

  8. Android水平仪实训报告,水准仪的实习报告(精选5篇)

    水准仪的实习报告(精选5篇) 一段忙碌又充实的实习生活又告一段落了,想必都收获了成长和成绩,让我们一起来学习写实习报告吧.在写之前,可以先参考范文喔!以下是小编为大家整理的水准仪的实习报告(精选5篇) ...

  9. Android水平仪实训报告,水准仪的实训报告.doc

    水准仪的实训报告 实验三 水准仪的使用练习 一.目的与要求 1.了解DS3级水准仪的基本构造,认识其主要部件的名称及使用. 2.练习水准仪的安置.瞄准与读数. 3.测定地面两点间高差. 二.组织和学时 ...

最新文章

  1. 远程控制软件VNC教程和对内网机器控制的实现
  2. Ubuntu 12.04 下安装 VirtualBox 及虚拟机winxp的安装
  3. 常用的正则表达式总结
  4. STM32的USART中断死循环,形成死机。
  5. Python+Selenium操作select下拉框
  6. Python学习---django知识补充之CBV
  7. AOSP6.0.1 系统中增加新的category类型与app绑定,并在hotseat容器中加载app
  8. 2021-11-10如何快速部署Ceph分布式高可用集群
  9. 有的编译器,for (i=1; i 《 1; i++)会执行循环体
  10. i7处理器好吗_i5和i7区别有多大,性能差距大吗?i59400F和i79700F的区别对比
  11. 基于ADS软件的阻抗匹配
  12. JZOJ 4250. 【五校联考7day1附加题】路径(折半搜索)
  13. 微软:区块链BaaS平台将成为重要盈…
  14. 德威控股在港交所招股书失效:2021年度利润大增,由杨时家族控股
  15. 50台同样配置的计算机装系统,几十台PC如何同时安装系统
  16. 提升汽车APP用户体验,火山引擎APMPlus的“独家秘笈”
  17. 如何向数组里添加元素
  18. php制作日历带节日实验目的,PHP 制作的日历一份
  19. 前端登录和注册页面的实现及验证
  20. webRTC原理及信令简介

热门文章

  1. [MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现
  2. UVa 988 - Many Paths, One Destination
  3. 如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便)
  4. 请问,现在android流行什么开源框架?
  5. mysql 索引_MySQL之索引
  6. sql算术运算符_SQL中的算术运算符
  7. FYFG的完整形式是什么?
  8. repeated_Ruby中带有示例的Array.repeated_combination()方法
  9. android web通讯录,Android手机开发之通讯录
  10. express 项目生成器_用于项目的Express模板生成器(2)| 应用程序结构研究